Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrNotAStructPtr is returned if you pass something that is not a pointer to a // Struct to Parse ErrNotAStructPtr = errors.New("env: expected a pointer to a Struct") )
nolint: gochecknoglobals
Functions ¶
func Parse ¶
func Parse(v interface{}) error
Parse parses a struct containing `env` tags and loads its values from environment variables.
Example ¶
type inner struct { Foo string `env:"FOO" envDefault:"foobar"` } type config struct { Home string `env:"HOME,required"` Port int `env:"PORT" envDefault:"3000"` IsProduction bool `env:"PRODUCTION"` Inner inner Hosts []string `env:"HOSTS" envDefault:"3000:888" envSeparator:":"` TempFolder string `env:"TEMP_FOLDER" envDefault:"${HOME}/tmp" envExpand:"true"` } os.Setenv("HOME", "/tmp/fakehome") var cfg config if err := Parse(&cfg); err != nil { fmt.Println("failed:", err) } fmt.Printf("%+v", cfg)
Output: {Home:/tmp/fakehome Port:3000 IsProduction:false Inner:{Foo:foobar}}
func ParseWithFuncs ¶
func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc) error
ParseWithFuncs is the same as `Parse` except it also allows the user to pass in custom parsers.
Types ¶
type ParserFunc ¶
ParserFunc defines the signature of a function that can be used within `CustomParsers`
Click to show internal directories.
Click to hide internal directories.