A simple, zero-dependencies library to parse environment variables into structs
6,307
stars
449
commits
Go
primary language
Sep 4, 2026
updated
A simple, zero-dependencies library to parse environment variables into structs.
go get github.com/caarlos0/env/v11
type config struct {
Home string `env:"HOME"`
}
// parse
var cfg config
err := env.Parse(&cfg)
// parse with generics
cfg, err := env.ParseAs[config]()
You can see the full documentation and list of examples at pkg.go.dev.
Encore – the platform for building Go-based cloud backends.
[!CAUTION]
Unexported fields will be ignored by
env. This is by design and will not change.
Parse: parse the current environment into a typeParseAs: parse the current environment into a type using genericsParseWithOptions: parse the current environment into a type with custom optionsParseAsWithOptions: parse the current environment into a type with custom options and using genericsMust: can be used to wrap Parse.* calls to panic on errorGetFieldParams: get the env parsed options for a typeGetFieldParamsWithOptions: get the env parsed options for a type with custom optionsOut of the box all built-in types are supported, plus a few others that are commonly used.
Complete list:
boolfloat32float64int16int32int64int8intstringuint16uint32uint64uint8uinttime.Durationtime.Locationencoding.TextUnmarshalerurl.URLPointers, slices and slices of pointers, and maps of those types are also supported.
You may also add custom parsers for your types.
The following tags are provided:
env: sets the environment variable name and optionally takes the tag options described belowenvDefault: sets the default value for the fieldenvPrefix: can be used in a field that is a complex type to set a prefix to all environment variables used in itenvSeparator: sets the character to be used to separate items in slices and maps (default: ,)envKeyValSeparator: sets the character to be used to separate keys and their values in maps (default: :)env tag optionsHere are all the options available for the env tag:
,expand: expands environment variables, e.g. FOO_${BAR},file: instructs that the content of the variable is a path to a file that should be read,init: initialize nil pointers,notEmpty: make the field errors if the environment variable is empty,required: make the field errors if the environment variable is not set,unset: unset the environment variable after useThere are a few options available in the functions that end with WithOptions:
Environment: keys and values to be used instead of os.Environ()TagName: specifies another tag name to use rather than the default envPrefixTagName: specifies another prefix tag name to use rather than the default envPrefixDefaultValueTagName: specifies another default tag name to use rather than the default envDefaultRequiredIfNoDef: set all env fields as required if they do not declare envDefaultOnSet: allows to hook into the env parsing and do something when a value is setPrefix: prefix to be used in all environment variablesUseFieldNameByDefault: defines whether or not env should use the field name by default if the env key is missingFuncMap: custom parse functions for custom typesExamples are live in pkg.go.dev, and also in the example test file.
env is considered feature-complete.
I do not intent to add any new features unless they really make sense, and are requested by many people.
Eventual bug fixes will keep being merged.
env tags(top 30 of 66)
Go
99.4%
A simple, zero-dependencies library to parse environment variables into structs
6,307
stars
449
commits
Go
primary language
Sep 4, 2026
updated
A simple, zero-dependencies library to parse environment variables into structs.
go get github.com/caarlos0/env/v11
type config struct {
Home string `env:"HOME"`
}
// parse
var cfg config
err := env.Parse(&cfg)
// parse with generics
cfg, err := env.ParseAs[config]()
You can see the full documentation and list of examples at pkg.go.dev.
Encore – the platform for building Go-based cloud backends.
[!CAUTION]
Unexported fields will be ignored by
env. This is by design and will not change.
Parse: parse the current environment into a typeParseAs: parse the current environment into a type using genericsParseWithOptions: parse the current environment into a type with custom optionsParseAsWithOptions: parse the current environment into a type with custom options and using genericsMust: can be used to wrap Parse.* calls to panic on errorGetFieldParams: get the env parsed options for a typeGetFieldParamsWithOptions: get the env parsed options for a type with custom optionsOut of the box all built-in types are supported, plus a few others that are commonly used.
Complete list:
boolfloat32float64int16int32int64int8intstringuint16uint32uint64uint8uinttime.Durationtime.Locationencoding.TextUnmarshalerurl.URLPointers, slices and slices of pointers, and maps of those types are also supported.
You may also add custom parsers for your types.
The following tags are provided:
env: sets the environment variable name and optionally takes the tag options described belowenvDefault: sets the default value for the fieldenvPrefix: can be used in a field that is a complex type to set a prefix to all environment variables used in itenvSeparator: sets the character to be used to separate items in slices and maps (default: ,)envKeyValSeparator: sets the character to be used to separate keys and their values in maps (default: :)env tag optionsHere are all the options available for the env tag:
,expand: expands environment variables, e.g. FOO_${BAR},file: instructs that the content of the variable is a path to a file that should be read,init: initialize nil pointers,notEmpty: make the field errors if the environment variable is empty,required: make the field errors if the environment variable is not set,unset: unset the environment variable after useThere are a few options available in the functions that end with WithOptions:
Environment: keys and values to be used instead of os.Environ()TagName: specifies another tag name to use rather than the default envPrefixTagName: specifies another prefix tag name to use rather than the default envPrefixDefaultValueTagName: specifies another default tag name to use rather than the default envDefaultRequiredIfNoDef: set all env fields as required if they do not declare envDefaultOnSet: allows to hook into the env parsing and do something when a value is setPrefix: prefix to be used in all environment variablesUseFieldNameByDefault: defines whether or not env should use the field name by default if the env key is missingFuncMap: custom parse functions for custom typesExamples are live in pkg.go.dev, and also in the example test file.
env is considered feature-complete.
I do not intent to add any new features unless they really make sense, and are requested by many people.
Eventual bug fixes will keep being merged.
env tags(top 30 of 66)
Go
99.4%