caarlos0/env has 6,366 GitHub stars and 300 forks. GitHot measured -1 stars in 24 hours, +1 in 7 days, and +9 in 15 days. Last checked 2026-11-22 16:16 UTC. Last push 2026-11-05.
측정된 스타 증가
- in 24h
- -1
- in 7 days
- +1
- in 15 days
- +9
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
소개
환경 변수를 구조체로 파싱하는 간단한 무의존성 라이브러리
개발 도구README.md
Original
A simple, zero-dependencies library to parse environment variables into structs.
Installation
go get github.com/caarlos0/env/v11
Supported Go versions
env supports the two latest major Go releases, following the
Go release policy.
Getting started
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.
Used and supported by
Encore – the platform for building Go-based cloud backends.
Usage
Caveats
[!CAUTION]
Unexported fields will be ignored by
env. This is by design and will not change.
[!CAUTION]
An environment variable that is set but empty will fall back to the
envDefaultvalue, if any. NeitherrequirednornotEmptyguard against this:requiredis satisfied by the default, whilenotEmptyonly sees the value after the default has been applied.
Functions
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 wrapParse.*calls to panic on errorGetFieldParams: get theenvparsed options for a typeGetFieldParamsWithOptions: get theenvparsed options for a type with custom options
Supported types
Out of the box all built-in types are supported, plus a few others that are commonly used.
Complete list:
boolfloat32float64int16int32int64int8intstringuint16uint32uint64uint8uinttime.Durationtime.Locationencoding.TextUnmarshalerurl.URL
Pointers, slices and slices of pointers, and maps of those types are also supported.
You may also add custom parsers for your types.
Tags
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 field (also used when the environment variable is set but empty)envPrefix: 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 options
Here 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 use
Parse Options
There are a few options available in the functions that end with WithOptions:
Environment: keys and values to be used instead ofos.Environ()TagName: specifies another tag name to use rather than the defaultenvPrefixTagName: specifies another prefix tag name to use rather than the defaultenvPrefixDefaultValueTagName: specifies another default tag name to use rather than the defaultenvDefaultRequiredIfNoDef: set allenvfields as required if they do not declareenvDefaultOnSet: allows to hook into theenvparsing and do something when a value is setPrefix: prefix to be used in all environment variablesUseFieldNameByDefault: defines whether or notenvshould use the field name by default if theenvkey is missingFuncMap: custom parse functions for custom types
Documentation and examples
Examples are live in pkg.go.dev, and also in the example test file.
Current state
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.
Badges
Related projects
- envdoc - generate documentation for environment variables from
envtags
관련 프로젝트 살펴보기
불러오는 중…