You might want to listen to value sets and, for example, log something or do some other kind of logic.
()
| 177 | // You might want to listen to value sets and, for example, log something or do |
| 178 | // some other kind of logic. |
| 179 | func ExampleParseWithOptions_onSet() { |
| 180 | type config struct { |
| 181 | Home string `env:"HOME,required"` |
| 182 | Port int `env:"PORT" envDefault:"3000"` |
| 183 | IsProduction bool `env:"PRODUCTION"` |
| 184 | NoEnvTag bool |
| 185 | Inner struct{} `envPrefix:"INNER_"` |
| 186 | } |
| 187 | os.Setenv("HOME", "/tmp/fakehome") |
| 188 | var cfg config |
| 189 | if err := ParseWithOptions(&cfg, Options{ |
| 190 | OnSet: func(tag string, value interface{}, isDefault bool) { |
| 191 | fmt.Printf("Set %s to %v (default? %v)\n", tag, value, isDefault) |
| 192 | }, |
| 193 | }); err != nil { |
| 194 | fmt.Println("failed:", err) |
| 195 | } |
| 196 | fmt.Printf("%+v", cfg) |
| 197 | // Output: Set HOME to /tmp/fakehome (default? false) |
| 198 | // Set PORT to 3000 (default? true) |
| 199 | // Set PRODUCTION to (default? false) |
| 200 | // {Home:/tmp/fakehome Port:3000 IsProduction:false NoEnvTag:false Inner:{}} |
| 201 | } |
| 202 | |
| 203 | // By default, env supports anything that implements the `TextUnmarshaler` |
| 204 | // interface, which includes `time.Time`. |
nothing calls this directly
no test coverage detected