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

Function ExampleParseWithOptions_useFieldName

example_test.go:379–392  ·  view source on GitHub ↗

If you don't want to set the `env` tag on every field, you can use the `UseFieldNameByDefault` option. It will use the field name to define the environment variable name. So, `Foo` becomes `FOO`, `FooBar` becomes `FOO_BAR`, and so on.

()

Source from the content-addressed store, hash-verified

377// It will use the field name to define the environment variable name.
378// So, `Foo` becomes `FOO`, `FooBar` becomes `FOO_BAR`, and so on.
379func ExampleParseWithOptions_useFieldName() {
380 type Config struct {
381 Foo string
382 }
383 os.Setenv("FOO", "bar")
384 var cfg Config
385 if err := ParseWithOptions(&cfg, Options{
386 UseFieldNameByDefault: true,
387 }); err != nil {
388 fmt.Println(err)
389 }
390 fmt.Printf("%+v", cfg)
391 // Output: {Foo:bar}
392}
393
394// The `env` tag option `file` (e.g., `env:"tagKey,file"`) can be added in
395// order to indicate that the value of the variable shall be loaded from a

Callers

nothing calls this directly

Calls 1

ParseWithOptionsFunction · 0.85

Tested by

no test coverage detected