1.下载第三方库:go get gopkg.in/gcfg.v1
2.创建conf.yaml文件
enabled: true
path: d://
3.解析yaml的代码
package main import ( "fmt" "gopkg.in/yaml.v2" "io/ioutil" "log" ) func main(){ var c conf c.getConf() fmt.Println("path:" + c.Path) fmt.Print(c.Enabled) } type conf struct { Enabled bool `yaml:"enabled"` //yaml:yaml格式 enabled:属性的为enabled Path string `yaml:"path"` } // (c *conf)这个方法可以被conf结构体调用 func (c *conf) getConf() *conf { yamlFile, err := ioutil.ReadFile("conf.yaml") if err != nil { log.Printf("yamlFile.Get err #%v ", err) } err = yaml.Unmarshal(yamlFile, c) if err != nil { log.Fatalf("Unmarshal: %v", err) } return c }