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

Function ExampleParse_init

example_test.go:141–155  ·  view source on GitHub ↗

You can automatically initialize `nil` pointers regardless of if a variable is set for them or not. This behavior can be enabled by using the `init` tag option.

()

Source from the content-addressed store, hash-verified

139// is set for them or not.
140// This behavior can be enabled by using the `init` tag option.
141func ExampleParse_init() {
142 type Inner struct {
143 A string `env:"OLA" envDefault:"HI"`
144 }
145 type Config struct {
146 NilInner *Inner
147 InitInner *Inner `env:",init"`
148 }
149 var cfg Config
150 if err := Parse(&cfg); err != nil {
151 fmt.Println(err)
152 }
153 fmt.Print(cfg.NilInner, cfg.InitInner)
154 // Output: <nil> &{HI}
155}
156
157// You can define the default value for a field by either using the
158// `envDefault` tag, or when initializing the `struct`.

Callers

nothing calls this directly

Calls 1

ParseFunction · 0.85

Tested by

no test coverage detected