env alternatives and similar packages
Based on the "Configuration" category.
Alternatively, view env alternatives based on common mentions on social networks and blogs.
-
kelseyhightower/envconfig
Go library for managing configuration data from environment variables. -
konfig
Composable, observable and performant config handling for Go for the distributed processing era. -
koanf
Light weight, extensible library for reading config in Go applications. Built in support for JSON, TOML, YAML, env, command line. -
config
JSON or YAML configuration wrapper with environment variables and flags parsing. -
cleanenv
Minimalistic configuration reader (from files, ENV, and wherever you want). -
hjson
Human JSON, a configuration file format for humans. Relaxed syntax, fewer mistakes, more comments. -
gookit/config
application config manage(load,get,set). support JSON, YAML, TOML, INI, HCL. multi file load, data override merge. -
gcfg
read INI-style configuration files into Go structs; supports user-defined types and subsections -
joshbetz/config
A small configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP. -
goConfig
Parse a struct as input and populates the fields of this struct with parameters fom command line, environment variables and configuration file. -
harvester
Harvester, a easy to use static and dynamic configuration package supportig seeding, env vars and Consul integration. -
onion
Layer based configuration for Go, Supports JSON, TOML, YAML, properties, etcd, env, and encryption using PGP. -
configuro
opinionated configuration loading & validation framework from ENV and Files focused towards 12-Factor compliant applications. -
configure
Provides configuration through multiple sources, including JSON, flags and environment variables. -
configuration
Library for initializing configuration structs from env variables, files, flags and 'default' tag. -
go-up
A simple configuration library with recursive placeholders resolution and no magic. -
hocon
Configuration library for working with the HOCON(a human-friendly JSON superset) format, supports features like environment variables, referencing other values, comments and multiple files. -
CONFLATE
Library providing routines to merge and validate JSON, YAML and/or TOML files -
go-ssm-config
Go utility for loading configuration parameters from AWS SSM (Parameter Store). -
swap
Instantiate/configure structs recursively, based on build environment. (YAML, TOML, JSON and env). -
sprbox
Build-environment aware toolbox factory and agnostic config parser (YAML, TOML, JSON and Environment vars). -
gonfig
Tag-based configuration parser which loads values from different providers into typesafe struct. -
nasermirzaei89/env
Simple useful package for read environment variables. -
gone/jconf
Modular JSON configuration. Keep you config structs along the code they configure and delegate parsing to submodules without sacrificing full config serialization. -
txgruppi-config
Quick and easy way to load config files based on a simple set of rules.
Scout APM - Leading-edge performance monitoring starting at $39/month
Do you think we are missing an alternative of env or a related project?
Popular Comparisons
README
env
Simple lib to parse envs to structs in Go.
Example
A very basic example:
package main
import (
"fmt"
"time"
// if using go modules
"github.com/caarlos0/env/v6"
// if using dep/others
"github.com/caarlos0/env"
)
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT" envDefault:"3000"`
IsProduction bool `env:"PRODUCTION"`
Hosts []string `env:"HOSTS" envSeparator:":"`
Duration time.Duration `env:"DURATION"`
TempFolder string `env:"TEMP_FOLDER" envDefault:"${HOME}/tmp" envExpand:"true"`
}
func main() {
cfg := config{}
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
}
fmt.Printf("%+v\n", cfg)
}
You can run it like this:
$ PRODUCTION=true HOSTS="host1:host2:host3" DURATION=1s go run main.go
{Home:/your/home Port:3000 IsProduction:true Hosts:[host1 host2 host3] Duration:1s}
Supported types and defaults
Out of the box all built-in types are supported, plus a few others that are commonly used.
Complete list:
string
bool
int
int8
int16
int32
int64
uint
uint8
uint16
uint32
uint64
float32
float64
string
time.Duration
encoding.TextUnmarshaler
url.URL
Pointers, slices and slices of pointers of those types are also supported.
You can also use/define a custom parser func for any other type you want.
If you set the envDefault
tag for something, this value will be used in the
case of absence of it in the environment.
By default, slice types will split the environment value on ,
; you can change
this behavior by setting the envSeparator
tag.
If you set the envExpand
tag, environment variables (either in ${var}
or
$var
format) in the string will be replaced according with the actual value
of the variable.
Unexported fields are ignored.
Custom Parser Funcs
If you have a type that is not supported out of the box by the lib, you are able
to use (or define) and pass custom parsers (and their associated reflect.Type
)
to the env.ParseWithFuncs()
function.
In addition to accepting a struct pointer (same as Parse()
), this function
also accepts a map[reflect.Type]env.ParserFunc
.
env
also ships with some pre-built custom parser funcs for common types. You
can check them out [here](parsers/).
If you add a custom parser for, say Foo
, it will also be used to parse
*Foo
and []Foo
types.
This directory contains pre-built, custom parsers that can be used with env.ParseWithFuncs
to facilitate the parsing of envs that are not basic types.
Check the example in the go doc for more info.
Required fields
The env
tag option required
(e.g., env:"tagKey,required"
) can be added
to ensure that some environment variable is set. In the example above,
an error is returned if the config
struct is changed to:
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT" envDefault:"3000"`
IsProduction bool `env:"PRODUCTION"`
Hosts []string `env:"HOSTS" envSeparator:":"`
SecretKey string `env:"SECRET_KEY,required"`
}
From file
The env
tag option file
(e.g., env:"tagKey,file"
) can be added
to in order to indicate that the value of the variable shall be loaded from a file. The path of that file is given
by the environment variable associated with it
Example below
package main
import (
"fmt"
"time"
"github.com/caarlos0/env"
)
type config struct {
Secret string `env:"SECRET,file"`
Password string `env:"PASSWORD,file" envDefault:"/tmp/password"`
Certificate string `env:"CERTIFICATE,file" envDefault:"${CERTIFICATE_FILE}" envExpand:"true"`
}
func main() {
cfg := config{}
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
}
fmt.Printf("%+v\n", cfg)
}
$ echo qwerty > /tmp/secret
$ echo dvorak > /tmp/password
$ echo coleman > /tmp/certificate
$ SECRET=/tmp/secret \
CERTIFICATE_FILE=/tmp/certificate \
go run main.go
{Secret:qwerty Password:dvorak Certificate:coleman}
Options
Environment
By setting the Options.Environment
map you can tell Parse
to add those keys
and values
as env vars before parsing is done. These envs are stored in the map and never actually set by os.Setenv
.
This option effectively makes env
ignore the OS environment variables: only the ones provided in the option are used.
This can make your testing scenarios a bit more clean and easy to handle.
package main
import (
"fmt"
"log"
"github.com/caarlos0/env"
)
type Config struct {
Password string `env:"PASSWORD"`
}
func main() {
cfg := &Config{}
opts := &env.Options{Environment: map[string]string{
"PASSWORD": "MY_PASSWORD",
}}
// Load env vars.
if err := env.Parse(cfg, opts); err != nil {
log.Fatal(err)
}
// Print the loaded data.
fmt.Printf("%+v\n", cfg.envData)
}
Changing default tag name
You can change what tag name to use for setting the env vars by setting the Options.TagName
variable.
For example
package main
import (
"fmt"
"log"
"github.com/caarlos0/env"
)
type Config struct {
Password string `json:"PASSWORD"`
}
func main() {
cfg := &Config{}
opts := &env.Options{TagName: "json"}
// Load env vars.
if err := env.Parse(cfg, opts); err != nil {
log.Fatal(err)
}
// Print the loaded data.
fmt.Printf("%+v\n", cfg.envData)
}