Dotenv

A lightweight package for loading dot env (.env) files into structs for Go projects
Alternatives To Dotenv
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Dynaconf2,9534613918 hours ago80June 06, 2022111mitPython
Configuration Management for Python ⚙
Node Convict2,1631,0564462 months ago62May 07, 202266otherJavaScript
Featureful configuration management library for Node.js
Envkey39312164 days ago75November 02, 20229mitTypeScript
End-to-end encrypted configuration and secrets management
Config30412 months ago34June 19, 20222mitGo
A lightweight yet powerful configuration manager for the Go programming language
Fig24682 months ago2January 03, 20224apache-2.0Go
A minimalist Go configuration library
Fsconfig114
22 years ago23July 25, 20213unlicenseF#
FsConfig is a F# library for reading configuration data from environment variables and AppSettings with type safety.
Parse_it97113 days ago102July 05, 20224lgpl-3.0Python
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Envh966121 days ago9May 05, 2022mitGo
Go helpers to manage environment variables
Envyable7612562 years ago8November 08, 20162mitRuby
The simplest yaml to ENV config loader.
Ini7172 days ago32September 15, 2022mitGo
📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载
Alternatives To Dotenv
Select To Compare


Alternative Project Comparisons
Readme

GoDoc CI CodeQL Go Report Card Coverage Status

DotEnv

GoLobby DotEnv is a lightweight package for loading dot env (.env) files into structs for Go projects

Documentation

Supported Versions

It requires Go v1.16 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

IPS=192.168.0.1,192.168.0.2
IDS=10,11,12,13

DB_NAME=shop
DB_PORT=3306
DB_USER=root
DB_PASS=secret

Sample .go file:

type Config struct {
    Debug bool      `env:"DEBUG"`
    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"`
	IDs []int64  `env:"IDS"`
}

config := Config{}
file, err := os.Open(".env")

err = dotenv.NewDecoder(file).Decode(&config)

// Use `config` struct in your app!

Usage Tips

  • The Decode() function gets a pointer of a struct.
  • It ignores the fields that have no related environment variables in the file.
  • It supports nested structs and struct pointers.

Field Types

GoLobby DotEnv uses the GoLobby Cast package to cast environment variables to related struct field types. Here you can see the supported types:

golobby/cast

DotEnv Syntax

The following snippet shows a valid dot env file.

String = Hello Dot Env # Comment

# Quotes
Quote1="Quoted message!"
Quote2="You can use ' here"
Quote3='You can use " here'
Quote4="You can use # here"

# Booleans
Bool1 = true
Bool2 = 1 # true
Bool3 = false
Bool4 = 0 # false

# Arrays
Ints    = 1,2, 3, 4 , 5 # []int{1, 2, 3, 4, 5}
Strings = a,b, c, d , e # []string{"a", "b", "c", "d", "e"}
Floats  = 3.14,9.8, 6.9 # []float32{3.14, 9.8, 6.9}

See Also

  • GoLobby/Config: A lightweight yet powerful configuration management 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.

Popular Environment Variables Projects
Popular Configuration Management Projects
Popular Configuration Management Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Go
Golang
Environment Variables
Dotenv
Configuration Management