现在主流的配置文件格式有这么几种,xml、yaml、config… xml就算了,太挫了,太土, 太繁琐… config 就是mysql,apache my.cnf的那种格式,这个格式适合功能分层,不适合写同级的配置端.
yaml是我最喜欢的配置格式,像ansible、saltstack、puppet都是使用yaml来做配置格式. 我在以前的公司开发的平台系统用的都是yaml格式. 简练,充满张力 !!! 我在python中只用yaml格式,在golang中当然也会优先选择这门配置语言. 又废话了.
话说golang关于yaml的库包在github中能找到好几个,但有些yaml功能包光看那奇葩的库包名就觉得不靠谱. 说到这里我又要喷golang的库包管理了,你丫就不能整个类似pypi服务… 说实话我真的害怕作者提交新功能,然后导致整个库包都不能使用。
废话不多说了,直接上yaml的例子。 我在代码里参杂了yaml.v2库的使用方法.
跟golang处理json结构一样,yaml的格式也是需要用struct结构体反射的. struct里的字段首字母最好是大写,yaml string到时无所谓.
<span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; font-weight:800; font-size:18px!important; line-height:1.5!important">
</span>
<pre style="margin-top:0px; margin-bottom:0px; padding:0px; white-space:pre-wrap; word-wrap:break-word; background-color:rgb(164,215,227); font-family:"Courier New"!important; font-size:18px!important">#http:<span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,128,0); line-height:1.5!important">//</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,128,0); line-height:1.5!important">xiaorui.cc</span>
<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">package main
import (
</span>"fmt"
"log"
"gopkg.in/yaml.v2"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">
)
</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,0,255); line-height:1.5!important">var</span> data =<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important"> `
blog: xiaorui.cc
best_authors: [</span>"fengyun","lee","park"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">]
desc:
counter: </span>521<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">
plist: [</span>3, 4<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">]
`
type T struct {
Blog string
Authors []string `yaml:</span>"best_authors,flow"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">`
Desc struct {
Counter </span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,0,255); line-height:1.5!important">int</span> `yaml:"Counter"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">`
Plist []</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,0,255); line-height:1.5!important">int</span> `yaml:",flow"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">`
}
}
func main() {
t :</span>=<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important"> T{}
</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,128,0); line-height:1.5!important">//</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,128,0); line-height:1.5!important">把yaml形式的字符串解析成struct类型</span>
err := yaml.Unmarshal([]<span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,0,255); line-height:1.5!important">byte</span>(data), &<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">t)
</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,128,0); line-height:1.5!important">//</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,128,0); line-height:1.5!important">修改struct里面的记录</span>
t.Blog = "this is Blog"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">
t.Authors </span>= append(t.Authors, "myself"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">)
t.Desc.Counter </span>= 99<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">
fmt.Printf(</span>"--- t:\n%v\n\n"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">, t)
</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,128,0); line-height:1.5!important">//</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,128,0); line-height:1.5!important">转换成yaml字符串类型</span>
d, err := yaml.Marshal(&<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">t)
</span><span style="margin:0px; padding:0px; font-weight:800; color:rgb(0,0,255); line-height:1.5!important">if</span> err !=<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important"> nil {
log.Fatalf(</span>"error: %v"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">, err)
}
fmt.Printf(</span>"--- t dump:\n%s\n\n"<span style="margin:0px; padding:0px; font-weight:800; line-height:1.5!important">, string(d))
}</span></pre>
<span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; font-weight:800; font-size:18px!important; line-height:1.5!important">
</span>