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

Function ExampleParse_setDefaults

example_test.go:162–175  ·  view source on GitHub ↗

You can define the default value for a field by either using the `envDefault` tag, or when initializing the `struct`. Default values defined as `struct` tags will overwrite existing values during `Parse`.

()

Source from the content-addressed store, hash-verified

160// Default values defined as `struct` tags will overwrite existing values
161// during `Parse`.
162func ExampleParse_setDefaults() {
163 type Config struct {
164 Foo string `env:"DEF_FOO"`
165 Bar string `env:"DEF_BAR" envDefault:"bar"`
166 }
167 cfg := Config{
168 Foo: "foo",
169 }
170 if err := Parse(&cfg); err != nil {
171 fmt.Println(err)
172 }
173 fmt.Printf("%+v", cfg)
174 // Output: {Foo:foo Bar:bar}
175}
176
177// You might want to listen to value sets and, for example, log something or do
178// some other kind of logic.

Callers

nothing calls this directly

Calls 1

ParseFunction · 0.85

Tested by

no test coverage detected