当前位置:网站首页>Golang — 解析 yaml 文件
Golang — 解析 yaml 文件
2022-07-26 22:49:00 【itsgopher】
Golang — 解析 yaml 文件
yaml 文件是研发人员最常用的配置文件,yaml 文件的树形结构一直很受大家的欢迎。有过 SpringBoot 开发经验的同学对 yaml 非常熟悉,SpringBoot 整个项目的运行就需要一个 application.yaml 文件的支持,那么 Golang 项目中的 yaml 文件是如何解析的呢?Let`s dive in!
PS:根据 godocs 的说法,Golang 有三个强大的工具包支持 yaml 文件的解析,分别是:go-gypsygo-yamlgoccy-yaml。本文中我们将讨论其中 go-yaml 的用法。
对 yaml 解析源码感兴趣的同学请进入:go-yaml源码链接
Simple Demo
第一步,我们创建好项目后导入 go-yaml 依赖:
* go-yaml go get gopkg.in/yaml.v3
go: added gopkg.in/yaml.v3 v3.0.1
第二步,创建 main 文件并在内部编写一个简单的结构体:
type ConfDemo struct {
// 后面的 yaml 注解是在 yaml 文件中的属性名
A int `yaml:"a"`
B string `yaml:"b"`
C bool `yaml:"c"`
D []string `yaml:"d"`
E struct {
EA string `yaml:"ea"`
EB string `yaml:"eb"`
} `yaml:"e"`
}
第三步,在主目录下创建 conf 目录,并在 conf 目录下创建 conf_demo.yaml 文件去编写我们的配置:
a: 1
b: "I am B"
c: true
d:
- "I"
- "am"
- "D"
e:
ea: "I am EA"
eb: "I am EB"
第四步,编写 main 函数:
func main() {
// 读取文件所有内容装到 []byte 中
bytes, err := ioutil.ReadFile("config/conf_demo.yaml")
if err != nil {
log.Fatalln(err)
}
// 创建配置文件的结构体
var confDemo ConfDemo
// 调用 Unmarshall 去解码文件内容
// 注意要穿配置结构体的指针进去
err = yaml.Unmarshal(bytes, &confDemo)
if err != nil {
log.Fatalln(err)
}
// 调用 Unmarshall 对解码出来的 confDemo 进行编码
// 返回的 yml 是 []byte 类型的
yml, err := yaml.Marshal(confDemo)
if err != nil {
log.Fatalln(err)
}
// 输出结果
fmt.Printf("%#v\n", confDemo)
fmt.Printf("%s\n", yml)
}
第五步,运行并查看结果:
* go-yaml go run main.go
main.ConfDemo{
A:1, B:"I am B", C:true, D:[]string{
"I", "am", "D"}, E:struct {
EA string "yaml:\"ea\""; EB string "yaml:\"eb\"" }{
EA:"I am EA", EB:"I am EB"}}
a: 1
b: I am B
c: true
d:
- I
- am
- D
e:
ea: I am EA
eb: I am EB
go-yaml 其他解析方法
第一种解析方法即 simple Demo 中展现的 Marshall 和 Unmarshall 方法,他们会直接在结构体和字节流上进行操作。但有时我们为了图方便想把读取字节流这一步也交给组件去执行,这时候我们可以利用 yaml.Encoder 和 yaml.Decoder。
yaml.Encoder 和 yaml.Decoder 在 io.Writer 和 io.Reader 上进行操作读取其字节流并执行编码和解码的动作。我们将上面的例子以这种方法再次实现一遍:
func main() {
// 利用 os.Open 获取 File 对象,该对象实现了 io.Reader 和 io.Writer
file, err := os.Open("config/conf_demo.yaml")
if err != nil {
log.Fatalln(err)
}
// 构造新的 Decoder,并传入 file
decoder := yaml.NewDecoder(file)
// 配置文件结构体
var confDemo ConfDemo
// 解码操作,注意要传入地址
err = decoder.Decode(&confDemo)
// 输出解码结果
fmt.Printf("%#v\n", confDemo)
if err != nil {
log.Fatalln(err)
}
// 构造新的 Encoder,这里直接传入了 os.Stdout,代表结果直接输出到控制台
encoder := yaml.NewEncoder(os.Stdout)
// 编码并输出
err = encoder.Encode(confDemo)
if err != nil {
log.Fatalln(err)
}
}
运行结果:
* go-yaml go run main.go
main.ConfDemo{
A:1, B:"I am B", C:true, D:[]string{
"I", "am", "D"}, E:struct {
EA string "yaml:\"ea\""; EB string "yaml:\"eb\"" }{
EA:"I am EA", EB:"I am EB"}}
a: 1
b: I am B
c: true
d:
- I
- am
- D
e:
ea: I am EA
eb: I am EB
边栏推荐
- [explain C language in detail] takes you to play with the choice (Branch) structure
- Dynamic routing rip protocol experiment
- GAN的训练技巧:炼丹师养成计划 ——生成式对抗网络训练、调参和改进
- Test and open basic daily question brushing (continuous updating...)
- 第五讲—按键控制LED
- [FPGA tutorial case 29] the second DDS direct digital frequency synthesizer based on FPGA - Verilog development
- Static comprehensive experiment (comprehensive exercise of static route, loopback interface, default route, empty interface, floating static)
- Dynamic routing ofps protocol configuration
- Es specify user name and password when instantiating resthighlevelclient
- Enumerated valueof() method stepping on the pit
猜你喜欢

HCIA(网络初级综合实验练习)

Test and open basic daily question brushing (continuous updating...)

C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释

2022最新抖音直播监控(二)直播间流媒体下载

The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language

动态路由rip协议实验

ACM mode input and output exercise

最新C语言入门与进阶 -史上最全最详细的C语言教程!! 第一节-总览C语言概括
![C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!](/img/b9/ade9a808a3f6d24cd9825dc9b010c1.png)
C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!

C语言——字符和字符串、算术运算符、类型转换
随机推荐
Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan
OSPF static experiment
GAN的训练技巧:炼丹师养成计划 ——生成式对抗网络训练、调参和改进
ACM模式输入输出练习
OSPF静态大实验
Enumerated valueof() method stepping on the pit
Es specify user name and password when instantiating resthighlevelclient
OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)
The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language
2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room
高度塌陷解决方法
Is index reproduction text generation image is score quantitative experiment whole process reproduction inception score quantitative evaluation experiment step on the pit and avoid the pit process
HCIA动态路由OSPF实验
VLAN原理简述、具体实验配置
[FPGA tutorial case 30] DDS direct digital frequency synthesizer based on FPGA -- frequency accuracy analysis with MATLAB
[volatile principle] volatile principle
二层封装技术(HDLC、PPP--PAP\CHAP、GRE)实验练习
2022 latest Tiktok live broadcast monitoring (II) streaming media download in live broadcast room
Text to image论文精读GR-GAN:逐步细化文本到图像生成 GRADUAL REFINEMENT TEXT-TO-IMAGE GENERATION
7.8 锐捷网络笔试