Go读取yaml文件,struct返回,json输出

程序模块中,缺少不了一些配置定义,这时定义yaml是个很好的选择

先定义yaml文件内容,文件名为:task_status_config.yaml

复制代码
confs:
  #阅读类任务,即提醒任务
  read:
    name: read
    await: #待开始任务
      status_id: 0
    ing: #进行中任务
      receipt: #是否已阅读
        yes:
          status_id: 4 #已完成
        no:
          status_id: 1 #进行中
    timeout: #已逾期
      receipt: #是否已阅读
        yes:
          status_id: 8 #已逾期-已完成
        no:
          status_id: 2 #已逾期-未完成
  #回执类任务,即预警任务
  receipt:
    name: receipt
    await: #待开始任务
      status_id: 0
    ing: #进行中任务
      receipt: #是否已回执
        yes:
          status_id: 4 #已完成
        no:
          status_id: 1 #进行中
    timeout: #已逾期
      receipt: #是否已回执
        yes:
          status_id: 8 #已逾期-已完成
        no:
          status_id: 2 #已逾期-未完成
  #目标类任务,即系统任务
  target:
    name: target
    await: #待开始任务
      status_id: 0
    ing: #进行中任务
      receipt: #是否已回执
        yes:
          status_id: 3 #已回执
        no:
          status_id: 1 #进行中
    timeout: #已逾期
      receipt: #是否已回执
        yes:
          status_id: 7 #已逾期-已回执
        no:
          status_id: 2 #已逾期-未完成
    finish: #已完成任务
      status_id: 4 #已完成

编写程序读取yaml配置文件

Go 复制代码
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"gopkg.in/yaml.v2"
)

type TaskStatusConf struct {
	Confs map[string]*TaskClassify `yaml:"confs" json:"confs"`
}

type TaskClassify struct {
	Name    string        `yaml:"name" json:"name"`
	Await   StatusIdToInt `yaml:"await" json:"await"`
	Ing     TaskRecept    `yaml:"ing" json:"ing"`
	Timeout TaskRecept    `yaml:"timeout" json:"timeout"`
	Finish  StatusIdToInt `yaml:"finish" json:"finish"`
}

type StatusIdToInt struct {
	StatusId int `yaml:"status_id" json:"status_id"`
}

type TaskRecept struct {
	Receipt StatusReceipt `yaml:"receipt" json:"receipt"`
}

type StatusReceipt struct {
	Yes StatusIdToInt `yaml:"yes" json:"yes"`
	No  StatusIdToInt `yaml:"no" json:"no"`
}

func getYaml() {
	var c TaskStatusConf
	//读取yaml配置文件, 将yaml配置文件,转换struct类型
	conf := c.getConf()

	//将对象,转换成json格式
	data, err := json.Marshal(conf)

	if err != nil {
		fmt.Println("err:\t", err.Error())
		return
	}

	//最终以json格式,输出
	fmt.Println("data(string):\t", string(data))

	var res TaskStatusConf
	_ = json.Unmarshal(data, &res)
	// 以结构体形式访问
	fmt.Println(res.Confs["read"].Ing.Receipt.No.StatusId)
}

// 读取Yaml配置文件,
// 并转换成conf对象  struct结构
func (taskStatusConf *TaskStatusConf) getConf() *TaskStatusConf {
	//应该是 绝对地址
	yamlFile, err := os.ReadFile("../task_status_config.yaml")
	if err != nil {
		fmt.Println(err.Error())
	}
	//err = yaml.Unmarshal(yamlFile, kafkaCluster)
	err = yaml.UnmarshalStrict(yamlFile, taskStatusConf)
	if err != nil {
		fmt.Println(err.Error())
	}
	return taskStatusConf
}

运行结果:

相关推荐
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕3 天前
JSONObject与JSONArray封装数据格式区别
java·json
A黄俊辉A3 天前
uniapp webview中实现 app和内嵌的H5双向通信
vue.js·json
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
ttwuai4 天前
Go开源后台管理系统推荐:怎么按技术栈和边界比较4个官方仓库?
golang·gin
C语言小火车4 天前
C/C++ 为什么需要编译器?
开发语言·c++
codeejun4 天前
每日一Go·MySQL-5、锁机制全解析
云原生·golang
霍霍的袁4 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超4 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int4 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++