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

Function ExampleParseWithOptions_allFieldsRequired

example_test.go:260–275  ·  view source on GitHub ↗

Make all fields required by default.

()

Source from the content-addressed store, hash-verified

258
259// Make all fields required by default.
260func ExampleParseWithOptions_allFieldsRequired() {
261 type Config struct {
262 Username string `env:"EX_USERNAME" envDefault:"admin"`
263 Password string `env:"EX_PASSWORD"`
264 }
265
266 var cfg Config
267 if err := ParseWithOptions(&cfg, Options{
268 RequiredIfNoDef: true,
269 }); err != nil {
270 fmt.Println(err)
271 }
272 fmt.Printf("%+v\n", cfg)
273 // Output: env: required environment variable "EX_PASSWORD" is not set
274 // {Username:admin Password:}
275}
276
277// Set a custom environment.
278// By default, `os.Environ()` is used.

Callers

nothing calls this directly

Calls 1

ParseWithOptionsFunction · 0.85

Tested by

no test coverage detected