How do you unmarshal environment variables _N_ directly to slice? #2037
space88man
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem statement: I am trying to bind environment variables to struct with nested slice
X_Y_0_HOST, X_Y_0_POST, X_Y_1_HOST, X_Y_1_PORT to something like
Sidenote: from a Java/Spring Boot background this is how it unmarshals environment variables to field types of
List
/Array
. I am trying to emulate this behaviour so that the environment can be reused across Spring Boot/GoWhile using YAML lists
we can bind to a slice
Can this be done by environment variables with the notation:
# set prefix = X_ X_Y_0_HOST=localhost X_Y_0_PORT=12345 X_Y_1_HOST=abcdefg X_Y_1_PORT=12346
A bit of tinkering shows that the internal map for y is:
map[int]HostPost
instead of[]HostPort
; I always end up with an empty slice. I am using ExperimentalBindStruct + pure environment variables (no YAML schema preloaded).Preloading with
SetDefault("y.0.host"..)
,SetDefault("y.0.port"...), ...
"sorta" works withmap[int]HostPort
but the number of items is not fixed. Also it never seems to work with[]HostPort
.Beta Was this translation helpful? Give feedback.
All reactions