Handling slices of complex types.
()
| 297 | |
| 298 | // Handling slices of complex types. |
| 299 | func ExampleParse_complexSlices() { |
| 300 | type Test struct { |
| 301 | Str string `env:"STR"` |
| 302 | Num int `env:"NUM"` |
| 303 | } |
| 304 | type Config struct { |
| 305 | Foo []Test `envPrefix:"FOO"` |
| 306 | } |
| 307 | |
| 308 | os.Setenv("FOO_0_STR", "a") |
| 309 | os.Setenv("FOO_0_NUM", "1") |
| 310 | os.Setenv("FOO_1_STR", "b") |
| 311 | os.Setenv("FOO_1_NUM", "2") |
| 312 | |
| 313 | var cfg Config |
| 314 | if err := Parse(&cfg); err != nil { |
| 315 | fmt.Println(err) |
| 316 | } |
| 317 | fmt.Printf("%+v\n", cfg) |
| 318 | // Output: {Foo:[{Str:a Num:1} {Str:b Num:2}]} |
| 319 | } |
| 320 | |
| 321 | // Setting prefixes for inner types. |
| 322 | func ExampleParse_prefix() { |