Documentation
¶
Overview ¶
Package typedenv decodes OS environment variables into a struct
Example ¶
type config struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
Timeout time.Duration `env:"TIMEOUT"`
LogLevel slog.Level `env:"LOG_LEVEL,default=debug"`
}
os.Setenv("HOST", "localhost")
os.Setenv("PORT", "8080")
os.Setenv("TIMEOUT", "1s")
cfg, err := Load[config]()
if err != nil {
log.Fatalf("load config: %v", err)
}
fmt.Printf("%#v\n", cfg)
Output: typedenv.config{Host:"localhost", Port:8080, Timeout:1000000000, LogLevel:-4}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrNotStruct = errors.New("expected struct") ErrUnexportedField = errors.New("unexported field") ErrNotFound = errors.New("variable not found for key") ErrParse = errors.New("invalid value") ErrUnsupportedType = errors.New("unsupported type") ErrInvalidDefault = errors.New("invalid default value") ErrInvalidTag = errors.New("invalid env tag") )
Functions ¶
func Load ¶
Load reads operating system environment variables into a new instance of S, which must be a struct. Exported fields tagged with `env:"KEY"` are populated by looking up KEY in the environment; untagged fields are left at their zero value.
Supported field types: string, bool, the int and uint families, the float family, time.Duration, and url.URL.
Load returns an error if a tagged variable is missing from the environment, fails to parse, or targets an unexported field. Errors from multiple fields are joined.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.