Hai everyone, long time no see… I would like to share important information according to the title above.
We know that using env on golang project is very important and useful because with env we can manage the configuration in different development environments like development, staging and production. Without env we will have trouble changing the development environment from development to staging and vice versa.
In the golang project there are 2 libraries that are often used, godotenv and viper.
A Go port of Ruby's dotenv library (Loads environment variables from .env files)
GoDotEnv
A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file).
From the original Library:
Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.
But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv load variables from a .env file into ENV when the environment is bootstrapped.
It can be used as a library (for loading in env for your own daemons etc.) or as a bin command.
There is test coverage and CI for both linuxish and Windows environments, but I make no guarantees about the bin version working on Windows.
Note: Viper uses Go Modules to manage dependencies.
What is Viper?
Viper is a complete configuration solution for Go applications including 12-Factor apps
It is designed to work within an application, and can handle all types of configuration needs
and formats. It supports:
setting defaults
reading from JSON, TOML, YAML, HCL, envfile and Java properties config files
live watching and re-reading of config files (optional)
reading from environment variables
reading from remote config systems (etcd or Consul), and watching changes
go get github.com/spf13/viper
go get github.com/joho/godotenv
Using Viper
Create a new file main.go and copy the following code:
packagemainimport("fmt""os""github.com/spf13/viper")funcmain(){viper.AddConfigPath("env")viper.SetConfigType("env")viper.SetConfigName("app")err:=viper.ReadInConfig()// Find and read the config fileiferr!=nil{// Handle errors reading the config filepanic(fmt.Errorf("fatal error config file: %w",err))}fmt.Println("DB_HOST from config:",viper.GetString("DB_HOST"))fmt.Println("DB_PORT from config:",viper.GetString("DB_PORT"))}
Explained:
viper.AddConfigPath("env") location env on your project, this project env in env directory.
viper.SetConfigType("env") type of env file, in viper we use env, yaml and json file. This project uses env file.
viper.SetConfigName("app") Name of the env file. This project env file will have the name app.env. You can create env file like app.env or app.yaml or app.json.
Next to read the configuration from env file you need to add viper.ReadConfig(). Next, to read the contents of the env we use viper.getString and put the key env file.
Below is the contents of the env file:
DB_HOST=localhost
DB_PORT=5432
Run the project:
go run .
➜ golang-viper go run .
DB_HOST from config: localhost
DB_PORT from config: 5432
➜ golang-viper
Using Godotenv
Using this library is very simple and easy, just put the code below on your configuration file: