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

Function ExampleParseWithOptions_setEnv

example_test.go:279–296  ·  view source on GitHub ↗

Set a custom environment. By default, `os.Environ()` is used.

()

Source from the content-addressed store, hash-verified

277// Set a custom environment.
278// By default, `os.Environ()` is used.
279func ExampleParseWithOptions_setEnv() {
280 type Config struct {
281 Username string `env:"EX_USERNAME" envDefault:"admin"`
282 Password string `env:"EX_PASSWORD"`
283 }
284
285 var cfg Config
286 if err := ParseWithOptions(&cfg, Options{
287 Environment: map[string]string{
288 "EX_USERNAME": "john",
289 "EX_PASSWORD": "cena",
290 },
291 }); err != nil {
292 fmt.Println(err)
293 }
294 fmt.Printf("%+v\n", cfg)
295 // Output: {Username:john Password:cena}
296}
297
298// Handling slices of complex types.
299func ExampleParse_complexSlices() {

Callers

nothing calls this directly

Calls 1

ParseWithOptionsFunction · 0.85

Tested by

no test coverage detected