MCPcopy Create free account
hub / github.com/caarlos0/env / ExampleParse_complexSlices

Function ExampleParse_complexSlices

example_test.go:299–319  ·  view source on GitHub ↗

Handling slices of complex types.

()

Source from the content-addressed store, hash-verified

297
298// Handling slices of complex types.
299func 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.
322func ExampleParse_prefix() {

Callers

nothing calls this directly

Calls 1

ParseFunction · 0.85

Tested by

no test coverage detected