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`.
()
| 160 | // Default values defined as `struct` tags will overwrite existing values |
| 161 | // during `Parse`. |
| 162 | func 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. |