項目開發中,必不可少的即是,解析配置文件,本篇是在實際開發中,解析的Yaml配置文件的案例展示。mysql
Yaml配置文件展現:sql
mysql: url : "root@(127.0.0.1:3306)/IPInfoDb?charset=utf8&parseTime=True&loc=Local" dev: httpaddr : "0.0.0.0" httpport : 80
Golang語言解析代碼展現app
import "gopkg.in/yaml.v2" type YamlConf struct { MySql struct { Url string `yaml:"url"` } Dev struct { HttpAddr string `yaml:"httpaddr"` HttpPort string `yaml:"httpport"` } } func (this *YamlConf) GetConf() *YamlConf { YamlConf:=new(YamlConf) yamlFile, err := ioutil.ReadFile("/media/data/GoProject/IpInfoProject/src/conf/app.yaml") if err != nil { log.Printf("yaml open err is : \n %v \n", err) } err = yaml.Unmarshal(yamlFile, YamlConf) if err !=nil{ log.Printf("unmarshel err is \n %v \n",err) } return YamlConf }