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
}

运行结果:

相关推荐
疯狂SQL3 天前
手写高性能在线 JSON 工具|Web Worker 工程化打包 + 语法自动修复 + 多语言代码生成实战
typescript·json·next.js·web worker·前端性能优化·esbuild·源码实战
LDR0067 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术7 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园7 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob7 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享7 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.7 天前
C语言--day30
c语言·开发语言
何以解忧,唯有..7 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
謓泽7 天前
C语言不是语法,是通往机器的地图。
c语言·开发语言
云水一下7 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php