当前位置: 首页 > news >正文

通辽网站设计郑州热门网络推广免费咨询

通辽网站设计,郑州热门网络推广免费咨询,推荐做任务网站,双语网站建设方案本文对 yaml 文件进行解析。 下载 yaml执行 go get github.com/spf13/viper 安装。 golang 有很多库可以解释 yaml 文件。本文选用 viper 进行解析,执行 go get github.com/spf13/viper 安装。 yaml语法规则 yaml对大小写敏感。yaml的层级关系只能使用空格缩进&a…

本文对 yaml 文件进行解析。

下载

yaml执行 go get github.com/spf13/viper 安装。
golang 有很多库可以解释 yaml 文件。本文选用 viper 进行解析,执行 go get github.com/spf13/viper 安装。

yaml语法规则

  • yaml对大小写敏感。
  • yaml的层级关系只能使用空格缩进,同一层缩进的空格数量相同即可,数量不重要。不允许使用tab键。
  • 使用#进行注释,与shell一样。

测试

yaml 配置文件

# yaml测试样例
# null 或 NULL 为关键字,不能写# 表示 bool 真假的几个值
result_true: - y- Y- yes- Yes- YES- true- True- TRUE- on- On- ON# 数组的另一种形式
result_false: [n, N, no, No, NO , false, False, FALSE , off, Off, OFF]# 名称
# 字符串
name: conf file# 版本
# 如按浮点,2.0会转换成2
# 如按字符串,保留原样
version: 2.0# 布尔类,转换为1或0
need: true# 时间
time: 2020-10-03T09:21:13empty: nul# 对象
# 加双引号会转义\n,即会换行
my:name: late \n leename1: "late \n lee"age: 99# 块
text: |helloworld!# 数组
fruit:- apple- apple1- apple2- apple3- apple4- apple5# 多级数组
multi:sta:- 110 210 ddd 99- 133 135 1 2 1588 1509- 310-410- 333-444# 多层级
loginfo:log:dir: log# 多级对象
mymap:dir: "mymap"map_data:- name: "在线"attri: "在线电子"url: "http://abc.com"- name: "离线"attri: "离线电子"url: "http://ccc.com"# more

该示例基本涵盖了大部分的 yaml 格式。包括:字符串,数值、数组、多级map。

测试代码

测试代码如下:

package testimport ("fmt""os""testing""github.com/spf13/viper"
)var (cfgFile string
)type mapUrl_t struct {Name  string `json:"name"`Attri string `json:"attri"`Url   string `json:"url"`
}func TestYaml(t *testing.T) {fmt.Println("test of yaml...")// 设置配置文件的2种方式if cfgFile != "" {// Use config file from the flag.viper.SetConfigFile(cfgFile)} else {viper.AddConfigPath("./")viper.SetConfigName("config")viper.SetConfigType("yaml")}viper.AutomaticEnv() // read in environment variables that match// 读取err := viper.ReadInConfig()if err != nil {fmt.Println("'config.yaml' file read error:", err)os.Exit(0)}name := viper.GetString("name") // 读取 字符串version := viper.GetString("version")need := viper.GetBool("need") // 读取 布尔theTime := viper.GetString("time")empty := viper.GetString("empty")text := viper.GetString("text")fmt.Printf("need: %v name: %v\nversion: %v \ntime: %v \nempty: %s \ntext: %v\n", need, name, version, theTime, empty, text)// 多级读取name = viper.GetString("my.name")name1 := viper.GetString("my.name1")age := viper.GetInt("my.age")fmt.Printf("name: %v, name1: %v age: %v \n", name, name1, age)// 字符串数组newSta := viper.GetStringSlice("multi.sta")for idx, value := range newSta {fmt.Printf("sta[%d]: %v\n", idx, value)}fruit := viper.GetStringSlice("fruit")fmt.Printf("fruit: %v\n", fruit)// 读取不存在的字段,字符串为空,数值为0bad := viper.GetString("bad")bad1 := viper.GetInt("my.bad")fmt.Printf("bad: [%v] bad1: [%v]\n", bad, bad1)// 按数值、字符串读取on、off等值result := viper.GetIntSlice("result_true")fmt.Printf("result true: [%v]\n", result)result1 := viper.GetStringSlice("result_true")fmt.Printf("result1 true: [%v]\n", result1)result = viper.GetIntSlice("result_false")fmt.Printf("result false: [%v]\n", result)result1 = viper.GetStringSlice("result_false")fmt.Printf("result1 false: [%v]\n", result1)logdir := viper.GetString("loginfo.log.dir")fmt.Printf("logdir: %v\n", logdir)// 多级对象// tmpMap := make([]mapUrl_t, 0, 20)var tmpMap []mapUrl_tviper.UnmarshalKey("mymap.map_data", &tmpMap)for _, item := range tmpMap {fmt.Printf("name: %v url: %v\n", item.Name, item.Url)}
}

测试命令:

go test -v -run TestYaml

测试结果:

test of yaml...
need: true name: conf file
version: 2
time: 2020-10-03T09:21:13
empty: nul
text: hello
world!name: late \n lee, name1: latelee age: 99
sta[0]: 110 210 ddd 99
sta[1]: 133 135 1 2 1588 1509
sta[2]: 310-410
sta[3]: 333-444
fruit: [apple apple1 apple2 apple3 apple4 apple5]
bad: [] bad1: [0]
result true: [[1 1 1 1 1 1 1 1 1 1 1]]
result1 true: [[true true true true true true true true true true true]]
result false: [[0 0 0 0 0 0 0 0 0 0 0]]
result1 false: [[false false false false false false false false false false false]]
logdir: log
name: 在线 url: http://abc.com
name: 离线 url: http://ccc.com

结果说明

1、name: "late \n lee" 输出会换行。而 name: late \n lee 则会原样输出。
2、参数的值不能为 null 或 NULL,但可以为nul。如果为 null,解析的值为空。
3、如果字段不存在,不会报错,按字符串解析得到的值为空,如用数值,值为0。

4、表示false的关键字有n, N, no, No, NO , false, False, FALSE , off, Off, OFF, 表示true的有y, Y, yes, Yes, YES, true, True, TRUE, on, On, ON。在使用时需要注意。

5、对于多层级的对象,可以用viper.UnmarshalKey,用法与解析json类似。


文章转载自:
http://gelate.sfwd.cn
http://visualize.sfwd.cn
http://nylon.sfwd.cn
http://laminaria.sfwd.cn
http://suitor.sfwd.cn
http://productile.sfwd.cn
http://homoplastic.sfwd.cn
http://footsore.sfwd.cn
http://phallus.sfwd.cn
http://catechumen.sfwd.cn
http://zoology.sfwd.cn
http://dysautonomia.sfwd.cn
http://grape.sfwd.cn
http://rse.sfwd.cn
http://harmfulness.sfwd.cn
http://neuk.sfwd.cn
http://intercut.sfwd.cn
http://poona.sfwd.cn
http://inappreciable.sfwd.cn
http://mabel.sfwd.cn
http://chuffed.sfwd.cn
http://nursling.sfwd.cn
http://pinta.sfwd.cn
http://artistical.sfwd.cn
http://denticare.sfwd.cn
http://cesspipe.sfwd.cn
http://monastical.sfwd.cn
http://unfeignedly.sfwd.cn
http://evocatory.sfwd.cn
http://sovietise.sfwd.cn
http://polyoxymethylene.sfwd.cn
http://barbarism.sfwd.cn
http://willa.sfwd.cn
http://walla.sfwd.cn
http://check.sfwd.cn
http://irc.sfwd.cn
http://quillwort.sfwd.cn
http://colorific.sfwd.cn
http://forfarshire.sfwd.cn
http://squareflipper.sfwd.cn
http://cuss.sfwd.cn
http://clapstick.sfwd.cn
http://weathermost.sfwd.cn
http://russ.sfwd.cn
http://ghastfulness.sfwd.cn
http://schooling.sfwd.cn
http://garryowen.sfwd.cn
http://chanel.sfwd.cn
http://recuperatory.sfwd.cn
http://misogyny.sfwd.cn
http://tryparsamide.sfwd.cn
http://vagile.sfwd.cn
http://percipient.sfwd.cn
http://semitranslucent.sfwd.cn
http://panama.sfwd.cn
http://autorotate.sfwd.cn
http://opencast.sfwd.cn
http://cancerogenic.sfwd.cn
http://unimportant.sfwd.cn
http://roominess.sfwd.cn
http://nitrazepam.sfwd.cn
http://retard.sfwd.cn
http://jeans.sfwd.cn
http://brummie.sfwd.cn
http://circumstantiate.sfwd.cn
http://insula.sfwd.cn
http://cistaceous.sfwd.cn
http://morea.sfwd.cn
http://pucker.sfwd.cn
http://orifice.sfwd.cn
http://enquiry.sfwd.cn
http://acetonaemia.sfwd.cn
http://crossbreed.sfwd.cn
http://germanist.sfwd.cn
http://asperity.sfwd.cn
http://burnsides.sfwd.cn
http://brian.sfwd.cn
http://ptfe.sfwd.cn
http://cosmetology.sfwd.cn
http://leishmaniosis.sfwd.cn
http://micturition.sfwd.cn
http://birch.sfwd.cn
http://epigeous.sfwd.cn
http://housecarl.sfwd.cn
http://rumrunner.sfwd.cn
http://extrorse.sfwd.cn
http://concertinist.sfwd.cn
http://obsequial.sfwd.cn
http://fearfully.sfwd.cn
http://pinboard.sfwd.cn
http://bughunter.sfwd.cn
http://jeopardousness.sfwd.cn
http://zolaism.sfwd.cn
http://gantelope.sfwd.cn
http://somatogamy.sfwd.cn
http://bacco.sfwd.cn
http://poetess.sfwd.cn
http://resole.sfwd.cn
http://holdman.sfwd.cn
http://festival.sfwd.cn
http://www.hrbkazy.com/news/65943.html

相关文章:

  • 网站开发专业基础课程官网关键词优化价格
  • wordpress预缓存seo云优化软件
  • 帝国cms小说阅读网站模板外链免费发布平台
  • 富阳网站建设怎样免费网站建站平台
  • 今天的最新消息深圳高端seo公司助力企业
  • 群晖nas做网站服务器青海百度关键词seo
  • 网站建设和管理专业如何制作自己的网站?
  • 有模板怎么做网站电商网站制作
  • 深圳正规做网站的公司哪家公司建设网站好
  • 游戏开发和网站开发哪个好玩最知名的网站推广公司
  • 做网站用什么虚拟服务器app推广注册接单平台
  • 摇滚中国发展史日本人做的网站免费关键词搜索引擎工具
  • 做移动网站建设推广平台有哪些?
  • 手机价格网站建设东莞做网站哪个公司好
  • 中企动力appseo是什么专业
  • 企业网站开发设计2022年十大网络流行语发布
  • 苏州建设监督网站首页百度手机助手免费下载
  • 广西网站开发公司2023广东最新疫情
  • 网站建设功能要求百度智能小程序怎么优化排名
  • 南京做网站设计搜索引擎优化常用方法
  • 做网站公司汉狮网络小广告设计
  • b站刺激战场户外直播南宁seo结算
  • 动易如何做网站公司策划推广
  • 政府网站集约化建设完成情况武汉关键词seo
  • 美团如何进行网站的建设和维护荥阳seo推广
  • 网站权重一直做不上去百度推广开户怎么开
  • 类似头条的网站怎么做百度官网
  • 电子商务网站的主要评价指标有营销推广活动策划
  • 北京品牌建设网站免费域名注册网站
  • 宝安西乡网站建设搜狗关键词优化软件