在go使用yaml
一、基本语法
纯量
# 纯量是最基本的,不可再分的值,包括:字符串、布尔值、整数、浮点数、Null、时间、日期等
name: redis
version: 5.6
port: 6379
stdin: true
# 空值可以用null也可以使用~
image: null
video: ~
# 日期和时间在yaml中使用的格式是ISO 8601
data: 2022-07-16
time: 2022-07-16T20:37:45 08:00
# 对于string类型的字符串,如果一行写不下,可以像下面这么写
singleLineString: >
this is a very long string
another line
another line
# 上面这个字符串会被解析为 "this is a very long string another line another line\n"
singleLineString: >-
this is a very long string
another line
another line
# 上面这个字符串会被解析为 "this is a very long string another line another line"
multiLineString: |
this is a very long string
another line
another line
# 上面这个字符串会被解析为 "this is a very long string\nanother line\nanother line\n"
数组和对象
# 数组,数组的元素可以和它的父亲同一层级也可以有缩进
prots:
-6379
-6380
# 下面这种写法只适合于纯量,如果数组元素是复合类型就不适合这种写法
ports: [6379, 6380]
# 对象,对于一个对象的属性缩进得是一样的
container:
name: mysql
image: mysql
port: 1236
version: 5.7
一个稍微复杂的例子
# 一个稍微复杂的例子
apiVersion: V1
kind: Pod
metadata:
name: redis-stdin
spec:
containers:
- name: redis
image: redis
imagePullPolicy: Always
stdin: true
ports:
- containerPort: 6379
- hostPort: 6380
- name: mongodb
image: mongo: 4.4.3
imagePullPolicy: Always
stdin: false
ports:
- containerPort: 27017
- hostPort: 27017
二、使用go读取yaml文件,并转换为struct结构体
1、新建 conf.yaml 文件
config:
models:
- model1:
name: Contract1
schema: 'schema/contract_schema1.json'
- model2:
name: Contract2
schema: 'schema/contract_schema2.json'
acls:
- acl1:
model: Contract1
role: wx-org1.chainmaker.org
operation: create
action: ALLOW
- acl2:
model: Contract2
role: wx-org2.chainmaker.org
operation: create
action: forbid
2、新建 main.go 文件
//package conf
package main
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type Conf struct {
Config Config
}
type Config struct {
Models []Model
Acls []Acl
}
type Model struct {
Name string
Schema string
}
type Acl struct {
Model string
Role string
Operation string
Action string
}
func GetConf() Conf {
var conf Conf // 加载文件
yamlFile, err := ioutil.ReadFile("conf.yaml")
if err != nil {
fmt.Println(err.Error())
} // 将读取的yaml文件解析为响应的 struct
err = yaml.Unmarshal(yamlFile, &conf)
if err != nil {
fmt.Println(err.Error())
}
return conf
}
func main() {
conf := GetConf()
fmt.Println(conf.Config.Models[0].Name)
fmt.Println(conf.Config.Models[0].Schema)
fmt.Println(conf.Config.Models[1].Name)
fmt.Println(conf.Config.Models[1].Schema)
fmt.Println(conf.Config.Acls[0].Model)
fmt.Println(conf.Config.Acls[0].Role)
fmt.Println(conf.Config.Acls[0].Operation)
fmt.Println(conf.Config.Acls[0].Action)
fmt.Println(conf.Config.Acls[1].Model)
fmt.Println(conf.Config.Acls[1].Role)
fmt.Println(conf.Config.Acls[1].Operation)
fmt.Println(conf.Config.Acls[1].Action)
}
main.go这个文件中的结构体定义可以用下面这个在线工具通过.yaml文件快速生成
yaml2go
参考资料:
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfhjfg
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01