Go读取yaml文件到struct类的实现方法

目录
  • 1、yaml文件准备
  • 2、config配置类准备
  • 3、读取配置文件到配置类
    • 3.1、安装Viper组件
    • 3.2、golang** **代码编写

1、yaml文件准备

common:
   secretid: AKIDxxxxx
   secretKey: 3xgGxxxx
   egion: ap-guangzhou
   zone: ap-guangzhou-7
   InstanceChargeType: POSTPAID_BY_HOUR

2、config配置类准备

可以通过在线配置工具转换成struct

例如:https://www.printlove.cn/tools/yaml2go

代码:

type ConfigData struct {
   // 公共配置
   Common Common `yaml:"common"`
}

type Common struct {
   // 密钥id。密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
   SecretId string `yaml:"secretid"`
   // 密钥key
   SecretKey string `yaml:"secretKey"`
   // 地域
   Region string `yaml:"region"`
   // 可用区
   Zone string `yaml:"zone"`
   //实例计费模式。取值范围:PREPAID:预付费,即包年包月。POSTPAID_BY_HOUR:按小时后付费。
   InstanceChargeType string `yaml:"InstanceChargeType"`
}

3、读取配置文件到配置类

使用viper读取配置到配置类中

3.1、安装Viper组件

go install github.com/spf13/viper@latest

3.2、golang** **代码编写

yaml文件放在工程根目录的data文件夹中

package main

import (
   "bufio"
   "github.com/spf13/viper"
   "io"
   "os"
   "strings"
)

type ConfigData struct {
   // 公共配置
   Common Common `yaml:"common"`
}

type Common struct {
   // 密钥id。
   SecretId string `yaml:"secretid"`
   // 密钥key
   SecretKey string `yaml:"secretKey"`
   // 地域
   Region string `yaml:"region"`
   // 可用区
   Zone string `yaml:"zone"`
   //实例计费模式。取值范围:PREPAID:预付费,即包年包月。POSTPAID_BY_HOUR:按小时后付费。
   InstanceChargeType string `yaml:"InstanceChargeType"`
}

func InitConfigStruct(path string) *ConfigData {
   var ConfigData = &ConfigData{}
   vip := viper.New()
   vip.AddConfigPath(path)
   vip.SetConfigName("config")
   vip.SetConfigType("yaml")
   //尝试进行配置读取
   if err := vip.ReadInConfig(); err != nil {
      panic(err)
   }
   err := vip.Unmarshal(ConfigData)
   if err != nil {
      panic(err)
   }

   return ConfigData
}

func main(){
    configData := InitConfigStruct("./data/")
    secretId := configData.Common.SecretId
    secretKey := configData.Common.SecretKey
    fmt.Printf("secretId:%s\n", secretId)
    fmt.Printf("secretKey:%s\n", secretKey)

}
作者:周钦雄
出处:http://www.cnblogs.com/zhouqinxiong/

到此这篇关于Go读取yaml文件到struct类的实现方法的文章就介绍到这了,更多相关Go读取yaml文件 内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • golang读取各种配置文件(ini、json、yaml)

    目录 viper读取ini文件 viper读取json文件 viper读取yaml文件 日常项目中,读取各种配置文件是避免不了的,这里介绍一个能读取多种配置文件的库,viper viper读取ini文件 config := viper.New() config.AddConfigPath("./conf/") // 文件所在目录 config.SetConfigName("b") // 文件名 config.SetConfigType("ini"

  • Golang使用第三方包viper读取yaml配置信息操作

    Golang有很多第三方包,其中的 viper 支持读取多种配置文件信息.本文只是做一个小小demo,用来学习入门用的. 1.安装 go get github.com/spf13/viper 2.编写一个yaml的配置文件,config.yaml database: host: 127.0.0.1 user: root dbname: test pwd: 123456 3.编写学习脚本main.go,读取config.yaml配置信息 package main import ( "fmt&quo

  • Go语言读取YAML 配置文件的两种方式分享

    目录 前言 yaml.v3 包 读取 yaml 文件 viper 包 读取 yaml 文件 小结 前言 在日常开发中,YAML 格式的文件基本上被默认为是配置文件,其内容因为缩进带来的层级感看起来非常直观和整洁.本文将会对 YAML 内容的读取进行介绍. yaml.v3 包 yaml.v3 的包,可以让我们在 Go 里面轻松地操作 yaml 格式的数据(如将 yaml 格式转成结构体等).在使用 yaml.v3 包之前,我们需要先安装它: go get gopkg.in/yaml.v3 读取 y

  • golang如何通过viper读取config.yaml文件

    目录 1.导入依赖包 2.编写yaml文件 3.编写读取yaml文件的go文件 4.使用config对象 5.viper源码分析 1.导入依赖包 import (     "github.com/spf13/viper" ) 2.编写yaml文件 放在conf目录下,文件名叫config.yaml # TODO  本地调试时放开 KubeSphere_URL: http://192.168.103.48:3188 # TODO 部署到环境时放开 #KubeSphere_URL: htt

  • Go读取yaml文件到struct类的实现方法

    目录 1.yaml文件准备 2.config配置类准备 3.读取配置文件到配置类 3.1.安装Viper组件 3.2.golang** **代码编写 1.yaml文件准备 common: secretid: AKIDxxxxx secretKey: 3xgGxxxx egion: ap-guangzhou zone: ap-guangzhou-7 InstanceChargeType: POSTPAID_BY_HOUR 2.config配置类准备 可以通过在线配置工具转换成struct 例如:h

  • 详解Python读取yaml文件多层菜单

    需要用到的Python知识点 Python的对象属性方法: 用到字典{key:value}值的提取: 列表的增加: if循环结合break的使用: yaml文件读取: 代码如下: #!/usr/bin/python34 import sys,os,re,yaml,time #reload(sys) #sys.setdefaultencoding('utf-8') ######################对input输入字符类型判断并转化##################### def in

  • Python读取YAML文件过程详解

    这篇文章主要介绍了Python读取YAML文件过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 YAML语法 学习手册 Python读取方法: import yaml with open('demo1.yaml', 'r', encoding='utf-8') as f: file_content = f.read() content = yaml.load(file_content, yaml.FullLoader) print(con

  • python读取yaml文件后修改写入本地实例

    首先安装pip install ruamel.yaml 用于修改yaml文件 #coding:utf-8 from ruamel import yaml def up_yml(ip_server): with open('./../docker-compose-demo.yml', encoding="utf-8") as f: content = yaml.load(f, Loader=yaml.RoundTripLoader) # 修改yml文件中的参数 content['serv

  • Python读取yaml文件的详细教程

    yaml简介 1.yaml [ˈjæməl]: Yet Another Markup Language :另一种标记语言.yaml 是专门用来写配置文件的语言,非常简洁和强大,之前用ini也能写配置文件,看了yaml后,发现这个更直观,更方便,有点类似于json格式.在自动化测试用的相当多所以需要小伙伴们要熟练掌握 2.yaml基本语法规则: 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 #表示注释,从这个字符

  • python 读取yaml文件的两种方法(在unittest中使用)

    作者:做梦的人(小姐姐) 出处:https://www.cnblogs.com/chongyou/ python读取yaml文件使用,有两种方式: 1.使用ddt读取 2,使用方法读取ddt的内容,在使用方法中进行调用 1.使用ddt读取 @ddt.ddt class loginTestPage(unittest.TestCase):     @ddt.file_data(path)     @ddt.unpack     def testlogin(self,**kwargs):       

  • iOS读取txt文件出现中文乱码的解决方法

    一.情景描述: 后台给一个txt文件,编码是utf-8,在Mac电脑Xcode开发环境下读取txt文件内容,汉字会出现乱码,英文没有乱码这种情况. 二.尝试解决方法: 修改编码格式,尝试了NSUTF16StringEncoding,NSUTF8StringEncoding,NSASCIIStringEncoding编码等,出现的问题有时是中文乱码,有时是utf-8不能打开文件问题,最终问题都没能解决. 三.猜测原因: txt文件是从window电脑上创建,有可能和环境有关,第二,编码问题. 四.

  • java使用POI读取properties文件并写到Excel的方法

    本文实例讲述了java使用POI读取properties文件并写到Excel的方法.分享给大家供大家参考.具体实现方法如下: package com.hubberspot.code; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import

  • C#从文件流读取xml文件到DataSet并显示的方法

    本文实例讲述了C#从文件流读取xml文件到DataSet并显示的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: DataSet ds= new DataSet (); FileStream fs = new FileStream (Server.MapPath=("somexml.xml"),FileMode.Open,FileAccess.Read); ds.ReadXml (fs); DataGrid1.DataSource = ds; DataGrid1.D

  • PHP读取大文件末尾N行的高效方法推荐

    小文件几兆以内大小,都可以通过file()函数,将文件按行读入数组,在用array_pop取得最后一行,就可以了. 但是对于很大的文本文件来说,机器内存不够大,或者php本身memory_limit有限制,这个办法就不适用了,即使强行不限制,效率也是非常低的. 没有办法了吗?当然有,不过没有现成的函数了,需要自己动手了. 这里需要用到文件指针,学过C的应该知道指针式个嘛玩意,通俗的讲吧,PHP中通过fopen打开一个文件,这时候还没有读取文件,这时候指向的是文件开头,指针位置也就是0,当你通过f

随机推荐