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

Function ExampleParseWithOptions_onSet

example_test.go:179–201  ·  view source on GitHub ↗

You might want to listen to value sets and, for example, log something or do some other kind of logic.

()

Source from the content-addressed store, hash-verified

177// You might want to listen to value sets and, for example, log something or do
178// some other kind of logic.
179func ExampleParseWithOptions_onSet() {
180 type config struct {
181 Home string `env:"HOME,required"`
182 Port int `env:"PORT" envDefault:"3000"`
183 IsProduction bool `env:"PRODUCTION"`
184 NoEnvTag bool
185 Inner struct{} `envPrefix:"INNER_"`
186 }
187 os.Setenv("HOME", "/tmp/fakehome")
188 var cfg config
189 if err := ParseWithOptions(&cfg, Options{
190 OnSet: func(tag string, value interface{}, isDefault bool) {
191 fmt.Printf("Set %s to %v (default? %v)\n", tag, value, isDefault)
192 },
193 }); err != nil {
194 fmt.Println("failed:", err)
195 }
196 fmt.Printf("%+v", cfg)
197 // Output: Set HOME to /tmp/fakehome (default? false)
198 // Set PORT to 3000 (default? true)
199 // Set PRODUCTION to (default? false)
200 // {Home:/tmp/fakehome Port:3000 IsProduction:false NoEnvTag:false Inner:{}}
201}
202
203// By default, env supports anything that implements the `TextUnmarshaler`
204// interface, which includes `time.Time`.

Callers

nothing calls this directly

Calls 1

ParseWithOptionsFunction · 0.85

Tested by

no test coverage detected