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
}

运行结果:

相关推荐
水瓶丫头站住25 分钟前
Qt中QRadioButton的样式设置
开发语言·qt
关关钧2 小时前
【R语言】绘图
开发语言·python·r语言
.切切切 切萝卜2 小时前
【Content-Type详解、Postman中binary格式、json格式数据转原始二进制流等】
json
大吱佬2 小时前
解决每次 Maven Rebuild 后 Java 编译器版本变为 1.5
java·开发语言·maven
C#Thread3 小时前
C#上位机--循环语句
开发语言·c#
diemeng11193 小时前
2024系统编程语言风云变幻:Rust持续领跑,Zig与Ada异军突起
开发语言·前端·后端·rust
MelonTe3 小时前
使用Go复刻skiplist核心功能
redis·golang
软件黑马王子3 小时前
Unity游戏制作中的C#基础(3)加减乘除算术操作符,比较运算符,逻辑与,或运算符
开发语言·unity·c#