
DotEnv
GoLobby DotEnv is a lightweight package for loading OS environment variables into structs in Go projects.
Documentation
Supported Versions
It requires Go v1.11
or newer versions.
Installation
To install this package run the following command in the root of your project.
go get github.com/golobby/dotenv
Usage Example
Sample .env
file:
DEBUG=true
APP_NAME=MyApp
APP_PORT=8585
DB_NAME=shop
DB_PORT=3306
DB_USER=root
DB_PASS=secret
Sample .go
file:
type Config struct {
Debug bool `dotenv:"DEBUG"`
App struct {
Name string `dotenv:"APP_NAME"`
Port int16 `dotenv:"APP_PORT"`
}
Database struct {
Name string `dotenv:"DB_NAME"`
Port int16 `dotenv:"DB_PORT"`
User string `dotenv:"DB_USER"`
Pass string `dotenv:"DB_PASS"`
}
}
c := Config{}
f, err := os.Open(".env")
err = dotenv.Load(f, &c)
// Use `c` struct in your app!
See Also
- GoLobby/Config:
A lightweight yet powerful config package for Go projects
- GoLobby/Env:
A lightweight package for loading OS environment variables into structs for Go projects
License
GoLobby DotEnv is released under the MIT License.