1. Why configure files
1. Easy to edit, relatively concentrated configuration, and convenient to modify. In a system with large business volume, configuration will facilitate future generations to understand the architecture of the entire system.
2. Decouple business code and environment, and environment configuration variables do not invade the code level
3. Switching different environments is more convenient
2. Use configuration files in go
1、viper website address
2. Local installation
go get /spf13/viper
3. Simple reading of configuration files
package main import ( "fmt" "/spf13/viper" ) func main() { v := () ("") if err := (); err != nil { ("Read configuration file failed", err) } (("name")) }
4. Create a file at the same level as the file
name: "test"
5. Use go run file name to run the file, so that the file will not be found if it is run directly.
6. It is recommended that you can use the right click to run the project after configuration.
package main import ( "fmt" "/spf13/viper" "os" "path" ) func main() { // Get the project directory workDir, _ := () v := () // Read directly into the file directory ((workDir, "test/")) if err := (); err != nil { ("Read configuration file failed", err) } (("name")) }
3. Read using structure
1. Simple use
package main import ( "fmt" "/spf13/viper" "os" "path" ) type ServerConfig struct { Name string `mapstructure:"name"` } func main() { // Get the project directory workDir, _ := () v := () ((workDir, "test/")) if err := (); err != nil { ("Read configuration file failed", err) } serverConfig := ServerConfig{} if err := (&serverConfig); err != nil { ("Failed to parse the structure", err) } () }
2. Nesting of structures, it is very common to configure mysql database
// Define mysqltype MySQLConfig struct { Host string `mapstructure:"host"` Port int `mapstructure:"port"` Username string `mapstructure:"username"` Password string `mapstructure:"password"` } type ServerConfig struct { Name string `mapstructure:"name"` // Use nested, please note that this place should be the same as in yaml MySqlConfig MySQLConfig `mapstructure:"mysql"` } // Use directly()
4. Distinguish between development environment and production environment
1. Configure environment variables on a mac computer, export an environment variable in .bash_profile. Be careful to close goland and open again.
# vim .bash_profile export IS_DEV= true source .bash_profile
2. Define the method to directly obtain the environment variables
func GetEnvInfo(env string) bool { () return (env) } func main() { (GetEnvInfo("IS_DEV")) }
3. Create a file of the same in the directory, and store configurations in different environments.
4. Use environment variables to read the configuration complete code
func GetEnvInfo1(env string) bool { () return (env) } // Define mysqltype MySQLConfig struct { Host string `mapstructure:"host"` Port int `mapstructure:"port"` Username string `mapstructure:"username"` Password string `mapstructure:"password"` } type ServerConfig struct { Name string `mapstructure:"name"` // Use nested, please note that this place should be the same as in yaml MySqlConfig MySQLConfig `mapstructure:"mysql"` } func main() { // Get the project directory workDir, _ := () isDev := GetEnvInfo1("IS_DEV") configFileName := (workDir, "/") if isDev { configFileName = (workDir, "/") } v := () (configFileName) if err := (); err != nil { ("Read configuration file failed", err) } serverConfig := ServerConfig{} if err := (&serverConfig); err != nil { ("Failed to parse the structure", err) } () }
5. Integrate configuration files in gin
1. Create two files in the same directory as the same level
2. Create a config, global, initialize folder in the directory
3. Write the configuration using structure reading in the config/ file
package config // Define mysqltype MySQLConfig struct { Host string `mapstructure:"host"` Port int `mapstructure:"port"` Username string `mapstructure:"username"` Password string `mapstructure:"password"` } type ServerConfig struct { Name string `mapstructure:"name"` // Use nested, please note that this place should be the same as in yaml MySqlConfig MySQLConfig `mapstructure:"mysql"` }
4. Define global variables in global/file (maybe you need to get configuration in a file in the entire project)
var ( ServerConfig * = &{} )
5. Initialize the configuration in the initialize/config directory
func GetEnvInfo(env string) bool { () return (env) } func InitConfig() { workDir, _ := () isDev := GetEnvInfo("IS_DEV") configFileName := (workDir, "/") if isDev { configFileName = (workDir, "/") } v := () //How to set the file path (configFileName) if err := (); err != nil { panic(err) } err := (&) if err != nil { ("Read configuration failed") } (&) }
6. Initialize the configuration file and print out the information
func main() { // Initialize the configuration () () }
This is the end of this article about the configuration of environment variables in go projects. For more related contents of environment variable configuration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!