The waves washed the sand and washed the lead

Where a company, whether it is the flow of the staff always exist, especially in some of the larger companies, the peak period of induction, left hundreds of people every day is very common thing, especially in some special period, average daily flow of thousands of people are also possible, basically can be so, Fantasy in a company do to the old idea for absolute part is not too realistic, soldiers of the camp of the iron flow, batman, for the bottom flow of people in the big, after all is also no big deal, as long as the core team member is stable, still poised, in such circumstances, only by constantly strengthen their own ability, In order to get rid of the stable volume of the workplace environment.

Configuration file for GO

For most of the project, the early in the process of encoding may be due to a variety of reasons, the information of some important hard code in the code, as the development work continued, such harm will constantly exposed, for subsequent developers integrated the principles of good maintenance, eventually will need this code to the specified in the configuration file. Therefore, it is necessary to master some knowledge about common configuration files. The following sections describe common YAML, TOML, JSON, and INI files.


Yaml configuration file

//conf.yaml

host: localhost:3306
user: xiaowang
pwd: 123456
dbname: test

Copy the code

// Parse the code

import ( "fmt" "gopkg.in/yaml.v2" "io/ioutil" ) type conf struct { Host string `yaml:"host"` User string `yaml:"user"` Pwd string `yaml:"pwd"` Dbname string `yaml:"dbname"` } func (c *conf) getConf() *conf { yamlFile, err := ioutil.ReadFile("conf.yaml") if err ! = nil { fmt.Println(err.Error()) } err = yaml.Unmarshal(yamlFile, c) return c } func main() { var c conf conf := c.getConf() fmt.Println(conf.Host, conf.User, conf.Dbname) }Copy the code

2. Toml configuration file

/ / conf. Toml file

# id id = 3 # name name = "TigerwolfC" # weight weight = 58.0 # books = ["Golang", "C++", "Python"] Sex = true #friend friend [friend] Age = 28 Name = "chen_peggy"Copy the code

// Parse the file

import ( "fmt" "github.com/BurntSushi/toml" "log" ) type Person struct { ID uint32 Sex bool Name string Weight float32 Friend *Friends Books []string } type Friends struct { Age int Name string } func main() { var cp Person if _, err := toml.DecodeFile("conf.toml", &cp); err ! = nil { log.Fatal(err) } fmt.Println(cp.Name,cp.ID) }Copy the code

3. Json file

//conf.json

{
  "enabled": true,
  "path": "/usr/local"
}
Copy the code

// Parse the file

import ( "encoding/json" "fmt" "log" "os" ) type Conf struct { Enable bool Path string } func main() { file, err := os.Open("conf.json") if err ! = nil { log.Fatal(err) } defer file.Close() decorder := json.NewDecoder(file) var c Conf err = decorder.Decode(&c) if err ! = nil { log.Fatalln(err) } fmt.Println(c.Path, c.Enable) }Copy the code

4. Ini configuration file

//conf.ini

app_name = awesome web

# possible values: DEBUG, INFO, WARNING, ERROR, FATAL
log_level = DEBUG

[mysql]
ip = 127.0.0.1
port = 3306
user = dj
password = 123456
database = awesome

[redis]
ip = 127.0.0.1
port = 6381
Copy the code

// Parse the file

import ( "fmt" "github.com/go-ini/ini" "log" ) func main () { c, err := ini.Load("conf.ini") if err ! = nil { log.Fatal(err) } fmt.Println(c.Section("").Key("app_name").String()) fmt.Println(c.Section("").Key("log_level").String()) fmt.Println(c.Section("mysql").Key("ip").String()) }Copy the code

summary

In general, it is not very difficult. However, due to historical reasons, INI configuration files have certain defects. They are mostly used on Windows platforms, and JSON files cannot be annotated. It is also used less as a configuration file, while others such as TOMl and YAML are used more widely. I hope these knowledge points can be firmly mastered.