Skip to content

Option to allow setting empty values #406

@olee

Description

@olee

I would like to be able to use environment variables to set a default value to an empty string if the environment variable is set:

type MyConfig struct {
	Variable string `env:"VARIABLE"`
}

func NewConfig() (*MyConfig, error) {
	cfg := &MyConfig{
		Variable: "default",
	}
	if err := env.Parse(cfg); err != nil {
		return nil, err
	}
	return cfg, nil
}

If VARIABLE="" is set, the env package would just completely skip overwriting the default value to become empty as well.
Basically, I would like to have an option to skip this empty check here:

env/env.go

Lines 507 to 509 in 3a3ea05

if value != "" && (!opts.SetDefaultsForZeroValuesOnly || refField.IsZero()) {
return set(refField, refTypeField, value, opts.FuncMap)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions