A simple and zero-dependencies library to parse environment variables into structs
Load application environment variables from a .env
file into the current process.
package main
import (
"fmt"
"time"
"github.com/go-zoox/env"
)
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT,default=8080"`
Password string `env:"PASSWORD,unset"`
IsProduction bool `env:"PRODUCTION"`
Hosts []string `env:"HOSTS,separator=;"`
TempFolder string `env:"TEMP_FOLDER,default=/tmp"`
Duration time.Duration `env:"DURATION"`
StringInts map[string]int `env:"MAP_STRING_INT"`
}
func main() {
cfg := config{}
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
}
// or you can use generics
cfg, err := env.ParseAs[config]()
if err != nil {
fmt.Printf("%+v\n", err)
}
fmt.Printf("%+v\n", cfg)
}
- caarlos0/env
- joho/godotenv - dot env file
- ilyakaznacheev/cleanenv - struct tag
- JeremyLoy/config - ci