• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在go使用yaml

武飞扬头像
啃西瓜的小煤球
帮助1

一、基本语法

纯量

# 纯量是最基本的,不可再分的值,包括:字符串、布尔值、整数、浮点数、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
学新通

参考资料:

YAML 语言教程
go yaml 官方文档
Golang 读取yaml文件

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhjfg
系列文章
更多 icon
同类精品
更多 icon
继续加载