wego/config
introduce
wego/config is a GO language version of ini configuration file parsing tool. wego/config has the following characteristics:
First prepare the app Conf configuration file:
1)Provided GetString,GetInt...as well as MustrString,MustInt...Function to facilitate the acquisition of configuration data. 2)Support through struct of tag Automatically assign configuration data to struct Field for. 3)Supports the use of environment variables to configure the value of items.
install
go get github.com/haming123/wego/config
Get started quickly
First prepare the app Conf configuration file:
#apply name app_name = demo #Log level: 0 off 1 fat 2 error 3 warn 4 info 5 debug level = 5 #Get environment variables go_path = ${GOPATH} [mysql] db_name = demodb db_host = 127.0.0.1:3306 db_user = root db_pwd = demopwd
Next, let's see how to read the configuration file and get the value of the configuration item:
package main import ( "fmt" "wego/config" ) func main() { var cfg config.ConfigData err := config.ParseFile("./app.conf", &cfg) if err != nil { fmt.Println(err) return } //Get the value of configuration item through GetXXX val := cfg.GetString("app_name") if val.Error != nil { fmt.Println( val.Error) return } fmt.Println(val.Value) }
Read configuration items in Section
ini files are organized in sections. The partition starts with [name] and ends before the next partition. The contents before all partitions belong to the default partition ([root]). The following code is an example of reading the section configuration item:
func TestSectionGet(t *testing.T) { var cfg config.ConfigData err := config.ParseFile("./app.conf", &cfg) if err != nil { t.Error(err) return } //Get the value of configuration item through GetXXX val := cfg.Section("mysql").GetString("db_name") if val.Error != nil { t.Error(err) return } t.Log(val.Value) }
Reading of various types of data
To facilitate the acquisition of various types of configuration data, wego/config provides GetString, GetInt... And other functions, such as reading the following configuration files:
#Configuration items of various data types str_value = hello bool_value = true int_value = 99 float_value = 123.45
func TestIniGetXXX(t *testing.T) { var cfg config.ConfigData err := config.ParseFile("./app2.conf", &cfg) if err != nil { t.Error(err) return } val := cfg.GetString("str_value") if val.Error != nil { t.Error(val.Error) return } t.Log(val.Value) val_bool := cfg.GetString("bool_value") if val.Error != nil { t.Error(val_bool.Error) return } t.Log(val_bool.Value) val_int := cfg.GetString("int_value") if val.Error != nil { t.Error(val_int.Error) return } t.Log(val_int.Value) val_float := cfg.GetString("float_value") if val.Error != nil { t.Error(val_float.Error) return } t.Log(val_float.Value) }
Quick reading of data
Using the GetXXX function to read configuration items requires error judgment. Such code will be very cumbersome to write. For this purpose, wego/config provides the corresponding MustXXX method, which returns only one value,
At the same time, it can accept default parameters. If there is no corresponding configuration item or the configuration content cannot be converted, the default value is used as the return value.
func TestIniMustXXX(t *testing.T) { var cfg config.ConfigData err := config.ParseFile("./app2.conf", &cfg) if err != nil { t.Error(err) return } t.Log(cfg.MustString("str_value")) t.Log(cfg.MustBool("bool_value")) t.Log(cfg.MustInt("int_value")) t.Log(cfg.MustFloat("float_value")) }
Reading of array type data
wego/config also supports the reading of array type data. It is required that the array should be added to the ini file as a configuration item, and the array members should be separated by the specified separator (for example, ","):
#Array configuration item ints_value = 1,2,3,4,5
func TestGetArray(t *testing.T) { var cfg config.ConfigData err := config.ParseFile("./app2.conf", &cfg) if err != nil { t.Error(err) return } arr, err := cfg.GetInts("ints_value", ",") if err != nil { t.Error(err) return } t.Log(arr) }
Structure field mapping and data reading
wego/config obtains the mapping relationship between the struct field and the configuration item through the tag of struct, and can automatically assign values to the struct field through the mapping relationship. First, you need to specify the mapping relationship in the struct definition:
type DbConfig struct { MysqlHost string `ini:"db_host"` MysqlUser string `ini:"db_user"` MysqlPwd string `ini:"db_pwd"` MysqlDb string `ini:"db_name"` } type AppConfig struct { AppName string `ini:"app_name"` HttpPort uint `ini:"http_port;default=8080"` GoPath string `ini:"go_path"` DbParam DbConfig `ini:"mysql"` }
explain:
wego/config supports configuring the default value when defining the mapping relationship. If there is no configuration content during data parsing, the default value is used as the value of the field.
wego/config uses GetStruct to assign values to struct fields, for example:
func TestIniGetStruct(t *testing.T) { var cfg config.ConfigData err := config.ParseFile("./app.conf", &cfg) if err != nil { t.Error(err) return } var data AppConfig err = cfg.GetStruct(&data) if err != nil { t.Error(err) return } t.Log(data) }
You can also call the GetStruct function of the section to get the configuration content directly from the section, for example:
func TestIniGetSectionStruct(t *testing.T) { var cfg config.ConfigData err := config.ParseFile("./app.conf", &cfg) if err != nil { t.Error(err) return } var data DbConfig err = cfg.Section("mysql").GetStruct(&data) if err != nil { t.Error(err) return } t.Log(data) }