首先安装解析的第三方包:
go get gopkg.in/yaml.v2
示例:
package main
import (
"os"
"log"
"fmt"
"encoding/json"
"gopkg.in/yaml.v2"
)
type Config struct {
Test Test `yaml:"test"`
}
type Test struct {
User []string `yaml:"user"`
MQTT MQ `yaml:"mqtt"`
Http HTTP `yaml:"http"`
}
type HTTP struct {
Port string `yaml:"port"`
Host string `yaml:"host"`
}
type MQ struct {
Host string `yaml:"host"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
//read yaml config
//注:path为yaml或yml文件的路径
func ReadYamlConfig(path string) (*Config,error){
conf := &Config{}
if f, err := os.Open(path); err != nil {
return nil,err
} else {
yaml.NewDecoder(f).Decode(conf)