You can automatically initialize `nil` pointers regardless of if a variable is set for them or not. This behavior can be enabled by using the `init` tag option.
()
| 139 | // is set for them or not. |
| 140 | // This behavior can be enabled by using the `init` tag option. |
| 141 | func ExampleParse_init() { |
| 142 | type Inner struct { |
| 143 | A string `env:"OLA" envDefault:"HI"` |
| 144 | } |
| 145 | type Config struct { |
| 146 | NilInner *Inner |
| 147 | InitInner *Inner `env:",init"` |
| 148 | } |
| 149 | var cfg Config |
| 150 | if err := Parse(&cfg); err != nil { |
| 151 | fmt.Println(err) |
| 152 | } |
| 153 | fmt.Print(cfg.NilInner, cfg.InitInner) |
| 154 | // Output: <nil> &{HI} |
| 155 | } |
| 156 | |
| 157 | // You can define the default value for a field by either using the |
| 158 | // `envDefault` tag, or when initializing the `struct`. |