GoLobby Env is a lightweight package for loading OS environment variables into structs for Go projects.
It requires Go v1.16
or newer versions.
To install this package run the following command in the root of your project
go get github.com/golobby/env/v2
The following example demonstrates how to use GoLobby Env package.
type Config struct {
Debug bool `env:"DEBUG"` // Possible Values: "true", "false", "1", "0"
App struct {
Name string `env:"APP_NAME"`
Port int16 `env:"APP_PORT"`
}
Database struct {
Name string `env:"DB_NAME"`
Port int16 `env:"DB_PORT"`
User string `env:"DB_USER"`
Pass string `env:"DB_PASS"`
}
IPs []string `env:IPS` // Possible Value: "192.168.0.1, 192.168.0.2"
IDs []int32 `env:IDS` // Possible Value: "10, 11, 12"
}
config := Config{}
err := env.Feed(&config)
// Use `config` struct in your app!
Feed()
function gets a pointer of a struct.GoLobby Env uses the GoLobby Cast package to cast OS environment variables to related struct field types. Here you can see the supported types:
GoLobby Env is released under the MIT License.