Gin Web framework, developed on the basis of Moose-Go

In actual development, the configuration file formats properties, YAML, JSON, ini are commonly used

Parse yamL files

Using third-party modules

Github.com/go-yaml/yam…

The installation

go get gopkg.in/yaml.v2
Copy the code

Two methods

Marshal serialization Unmarshal deserialization

use

data
name: Jiang jing
age: 18
hobby: ["code"."read"]
Copy the code
The structure of the body
type Info struct {
  Name  string
  Age   int
  Hobby []string
}
Copy the code
The test case

func TestTemplate(t *testing.T) {

	type Info struct {
		Name  string
		Age   int
		Hobby []string
	}

	data := 'name: Riverview age: 18 Hobby: ["code", "read"]'

	info := Info{}
	err := yaml.Unmarshal([]byte(data), &info)
	iferr ! =nil {
		t.Log(err)
		return
	}

	t.Log(info)
}
Copy the code
Run the test case
λ go test -v.\test\yaml_test.go === RUN TestTemplate yaml_test.go:33: {code read]} -- PASS: TestTemplate (0.00s) PASS OK command-line-arguments 0.040sCopy the code

Replacing the JSON configuration

The json file format used to configure database connection and application information has been replaced with yamL configuration file format

New application – dev. Yml

app:
  name: moose-go
  mode: debug

mysql:
  host: "127.0.0.1"
  port: "3306"
  driverName: mysql
  username: root
  password: "123456"
  database: moose-go

redis:
  host: localhost:6379
Copy the code

Define the corresponding structure

package model

type AppConfig struct {
	App struct {
		Name string `yaml:"name" json:"name"`
		Mode string `yaml:"mode" json:"mode"`
	}

	MySql struct {
		Host       string `yaml:"host" json:"host"`
		Port       string `yaml:"port" json:"port"`
		DriverName string `yaml:"driverName" json:"driverName"`
		UserName   string `yaml:"username" json:"username"`
		Password   string `yaml:"password" json:"password"`
		DataBase   string `yaml:"database" json:"database"`
	}

	Redis struct {
		Host string `yaml:"host" json:"host"`}}Copy the code

Encapsulation parsing method

var _yamlCfg *model.AppConfig = nil

func GetYamlConfig(a) *model.AppConfig {
	return _yamlCfg
}

func ParseYamlConfig(path string) *model.AppConfig {

	file, err := os.Open(path)
	iferr ! =nil {
		panic(err)
	}

	defer file.Close()

	data, err := ioutil.ReadAll(file)

	err = yaml.Unmarshal([]byte(data), &_yamlCfg)

	iferr ! =nil {
		panic(err)
	}

	log.Println(_yamlCfg)

	return _yamlCfg
}

Copy the code

use

Replace the original JSON format configuration

main.go

func init(a) {
	// util.ParseJSONConfig("./config/application-dev.json")

  // Read the file
	util.ParseYamlConfig("./config/application-dev.yml")
	// util.ParseYamlConfig("./config/application-prod.yml")
}
Copy the code

orm_engine.go

/ /...

func NewOrmEngine(a) {

  // Get structure information from the read file
	appInfo := util.GetYamlConfig()

	url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s? charset=utf8", appInfo.MySql.UserName, appInfo.MySql.Password, appInfo.MySql.Host, appInfo.MySql.Port, appInfo.MySql.DataBase)
	engine, err := xorm.NewEngine(appInfo.MySql.DriverName, url)

	iferr ! =nil {
		log.Panic(err.Error())
		return
	}

/ /...
Copy the code

Pay attention to the point

  • Define structure, define field must begin with uppercase, such as name ==> name
  • To define a structure, you can use a tag tag, such as Name Stringyaml:"name"
  • Reading configuration file structures must be consistent with defining structures