教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 golang使用yaml格式解析构建配置文件

golang使用yaml格式解析构建配置文件

发布时间:2022-01-29   编辑:jiaochengji.com
教程集为您提供golang使用yaml格式解析构建配置文件等资源,欢迎您收藏本站,我们将为您提供最新的golang使用yaml格式解析构建配置文件资源
<h1 class="postTitle" style="margin:0px 0px 20px; padding:0px; font-size:28px; font-weight:400; line-height:1.8; color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif"> golang使用yaml格式解析构建配置文件</h1>

现在主流的配置文件格式有这么几种,xml、yaml、config…  xml就算了,太挫了,太土, 太繁琐… config 就是mysql,apache my.cnf的那种格式,这个格式适合功能分层,不适合写同级的配置端.


yaml是我最喜欢的配置格式,像ansible、saltstack、puppet都是使用yaml来做配置格式.  我在以前的公司开发的平台系统用的都是yaml格式. 简练,充满张力 !!!  我在python中只用yaml格式,在golang中当然也会优先选择这门配置语言. 又废话了.

 

话说golang关于yaml的库包在github中能找到好几个,但有些yaml功能包光看那奇葩的库包名就觉得不靠谱. 说到这里我又要喷golang的库包管理了,你丫就不能整个类似pypi服务… 说实话我真的害怕作者提交新功能,然后导致整个库包都不能使用。

 

这个是我使用的go yaml包,star关注值还不低 . https://github.com/go-yaml/yaml 

废话不多说了,直接上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>

到此这篇关于“golang使用yaml格式解析构建配置文件”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
golang中解析yaml配置文件
golang使用yaml格式解析构建配置文件
让我们一起认识YAML:YAML简介
Golang加载yaml类型配置文件问题
golang几种常用配置文件使用方法总结(yaml、toml、json、xml、ini)
如何读取yaml,json,ini等配置文件【Golang 入门系列九】
beego读取配置yaml配置文件
Golang——使用yaml配置文件
python 怎么读取yaml文件
golang整洁之道(一)

[关闭]
~ ~