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配置文件

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
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值