JamesNan 2019-06-27
官网解释
%YAML 1.2 --- YAML: YAML Ain't Markup Language What It Is: YAML is a human friendly data serialization standard for all programming languages.
yaml语法,请参考这位仁兄的,写的很详细。
系统环境: MacOS、vim 8.0 、golang :go version go1.8 darwin/amd64
vim first.yaml
内容如下:
SiteName: seeta SiteAddr: BeiJing Https: true Nginx: Port: 443 LogPath: "/var/log//nginx/nginx.log" Path: "/opt/nginx/"
本文采用 : gopkg.in/yaml.v2
import "gopkg.in/yaml.v2"
package main import ( "fmt" "io/ioutil" "gopkg.in/yaml.v2" ) //Nginx nginx 配置 type Nginx struct { Port int `yaml:"Port"` LogPath string `yaml:"LogPath"` Path string `yaml:"Path"` } //Config 系统配置配置 type Config struct{ Name string `yaml:"SiteName"` Addr string `yaml:"SiteAddr"` HTTPS bool `yaml:"Https"` SiteNginx Nginx `yaml:"Nginx"` } func main() { var setting Config config, err := ioutil.ReadFile("./first.yaml") if err != nil { fmt.Print(err) } yaml.Unmarshal(config,&setting) fmt.Println(setting.Name) fmt.Println(setting.Addr) fmt.Println(setting.HTTPS) fmt.Println(setting.SiteNginx.Port) fmt.Println(setting.SiteNginx.LogPath) fmt.Println(setting.SiteNginx.Path) }
函数简介:
第一个输入参数:获取 ioutil.ReadFile 的值,为[]byte类型
第二个参数输出参数,可以为任意类型。