Decoding complex types
mainEnvconfig supports several ways to decode environment variable strings into complex Go types:
- Durations:
time.Durationvalues are parsed as standard Go duration strings (e.g.,"10m"). - Interfaces: Types implementing
TextUnmarshaler,BinaryUnmarshaler,json.Unmarshaler, orgob.Decoderare processed using those respective methods. - Slices: Decoded from comma-separated values (e.g.,
"a,b,c"). Byte slices are treated as strings. - Maps: Decoded from comma-separated
key:valuepairs (e.g.,"a:b,c:d"). You can customize the separator using theseparatortag. - Custom Decoders: Implement the
EnvDecode(ctx context.Context, val string) errormethod on your type to define custom logic.
type MyCustomType struct {
value string
}
func (t *MyCustomType) EnvDecode(ctx context.Context, val string) error {
resolved := someComplexFunction(val)
t.value = resolved
return nil
}