Golang development: class library (four) the use of configuration file parser goconfig

Why use goconfig to parse configuration files

At present, each language framework basically writes the configuration file in the same way. Basically, it first configures some basic variables. The basic variables contain the environment configuration, and then the environment variables are used to obtain the variables in the environment. For example, the production environment and the test environment use the same configuration, but the values ​​of variables in the corresponding environment are different, and the corresponding key value is obtained through the environment. It doesn't matter if you don't understand, you will understand when giving an example.

PHP framework yaf. The golang framework beego. The writing and analysis of the configuration are basically the same.

Look at the explanation of goconfig

goconfig is an easy-to-use, comment-supporting Go language configuration file parser. The format of the file is the same as the INI file under Windows.
The configuration file is composed of sections in the form of [section], and key-value pairs such as name:value or name=value are used internally; blank symbols at the beginning and end of each line will be ignored; if no section is specified, it will be placed by default Into the section named DEFAULT; you can use ";" or "#" as the beginning of the comment, and it can be placed on any single line.

Give a chestnut for a glance

How to use goconfig

Write a common configuration first

;redis cache
USER_LIST = USER:LIST
MAX_COUNT = 50
MAX_PRICE = 123456
IS_SHOW = true

[test]
dbdns = root:@tcp(127.0.0.1:3306)

[prod]
dbdns = root:@tcp(172.168.1.1:3306)

Then we read this configuration

package main

import (
	"log"
	"github.com/Unknwon/goconfig"
	"fmt"
)

func main() {
	cfg, err := goconfig.LoadConfigFile("./conf.ini")
	if err != nil {
		log.Fatalf("无法加载配置文件:%s", err)
	}
	userListKey, err := cfg.GetValue("","USER_LIST")
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(userListKey)
	userListKey2,_ := cfg.GetValue(goconfig.DEFAULT_SECTION, "USER_LIST")
	fmt.Println(userListKey2)
	maxCount := cfg.MustInt("","MAX_COUNT")
	fmt.Println(maxCount)
	maxPrice := cfg.MustFloat64("","MAX_PRICE")
	fmt.Println(maxPrice)
	isShow := cfg.MustBool("","IS_SHOW")
	fmt.Println(isShow)

	db := cfg.MustValue("test","dbdns")
	fmt.Println(db)

	dbProd := cfg.MustValue("prod","dbdns")
	fmt.Println(dbProd)

	//set 值
	cfg.SetValue("","MAX_NEW","10")
	maxNew := cfg.MustInt("","MAX_NEW")
	fmt.Println(maxNew)

	maxNew1,err := cfg.Int("","MAX_NEW")
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(maxNew1)
}

cfg.DeleteKey("", "MAX_NEW")

Execute the result

go build -o test.bin
 ./test.bin
USER:LIST
USER:LIST
50
123456
true
root:@tcp(127.0.0.1:3306)
root:@tcp(172.168.1.1:3306)
10
10

Consistent with the expected result.

The usage in the document is very clear. The
function LoadConfigFile loads one or more files, and then returns a variable of type ConfigFile.
GetValue can simply get a value.
Methods like Bool, Int, Int64 will directly return the value of the specified type.
Methods starting with Must will not return an error, but will return a zero value when an error occurs.
SetValue can set a certain value.
DeleteKey can delete a key.
Finally, SaveConfigFile can save your configuration to the local file system.
Use the method Reload to reload your configuration file.

Several points of attention
1. The configuration symbol can be used = or:
2. A system constant can be set in the section part, the value of the test environment is test, and the value of the production environment is prod, so there is no need to distinguish environment variables when calling .
cfg.MustValue(ENV,"dbdns")

If you need to know more detailed usage, check the document
https://github.com/Unknwon/goconfig

Guess you like

Origin blog.csdn.net/feifeixiang2835/article/details/96555130