A simple Go library for loading configuration from .env files into structs using struct tags.
go get github.com/digvijay-tech/envconfigDefine a struct with envconfig tags:
type Config struct {
Name string `envconfig:"NAME"`
Age uint8 `envconfig:"AGE"`
IsEngineer bool `envconfig:"IS_ENGINEER"`
Stars float32 `envconfig:"GITHUB_STARS"`
}Create a .env file:
NAME=John Doe
AGE=30
IS_ENGINEER=true
GITHUB_STARS=1.5Load the configuration:
import "github.com/digvijay-tech/envconfig"
opts := envconfig.EnvOptions[Config]{
FileName: ".env", // optional, defaults to ".env"
FilePath: ".", // optional, defaults to current directory
Schema: &Config{},
}
config, err := envconfig.Configure(opts)
if err != nil {
log.Fatal(err)
}stringint,int8,int16,int32,int64uint,uint8,uint16,uint32,uint64float32,float64bool
- Keys must be valid environment variable names (letters, numbers, underscores)
- Values can be quoted with single or double quotes (optional)
- Comments start with
# - Empty lines are ignored
- Invalid keys are skipped