首页 > 其他分享 >Go读取yaml文件到struct类

Go读取yaml文件到struct类

时间:2023-02-06 13:23:31浏览次数:34  
标签:secretKey struct ConfigData yaml Common Go string

Go读取yaml文件到struct类

原创 周钦雄 程序猿牧场 2023-01-10 21:34 发表于广东

1、yaml文件准备

common:  secretid: AKIDxxxxx  secretKey: 3xgGxxxx  region: 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)
}

标签:secretKey,struct,ConfigData,yaml,Common,Go,string
From: https://www.cnblogs.com/cheyunhua/p/17095102.html

相关文章

  • Go web中修改操作需要注意的一点
    不仅是Gin框架,曾经在http库中也遇到过类似的问题,所以进行详细的分析!定位到前端代码:bubble_frontend/TodoList.vueatmaster·Q1mi/bubble_frontend(github.com)handl......
  • 客服系统即时通讯IM开发(六)Glang Gorm 执行原生Sql语句增删改查封装库【唯一客服】网站
    在开发在线客服系统的时候,有某些地方需要使用脚本去批量执行SQL语句,这个时候就需要使用简单的执行SQL的封装函数了查询操作是使用的原生的sql库,没用Gorm,原因是Gorm的RawSca......
  • GO 的泛型前世今生
    转载于:https://blog.csdn.net/nihaihaoma/article/details/1256016302022年3月15日,争议非常大但同时也备受期待的泛型终于伴随着Go1.18发布了。可是因为Go对泛型的支持时......
  • GO 泛型的简单使用
    泛型的作用有关go泛型的提案和具体使用:https://github.com/polaris1119/go_dynamic_docs/blob/master/go2draft-contracts.md泛型生命周期只在编译期,旨在为程序员生......
  • struct与class的区别
    之前我解释过struct和class除了在默认的权限方面有一些区别之外就没有区别了,但是这只是c++。在c语言中class可以传递this指针,编译器看到class之后会在代码中用寄存器edx或......
  • (转)golang常用库之-标准库 sync包| go语言如何实现单例模式、读写锁(sync.RWMutex)
    原文:https://blog.csdn.net/inthat/article/details/124218961golang常用库之-标准库sync包Golangsync包提供了基础的异步操作方法,包括互斥锁Mutex,执行一次Once和并发等......
  • (转)深入浅出Golang Runtime
    原文:https://www.cnblogs.com/lovezbs/p/14467801.html本文为腾讯NOW直播研发工程师郝以奋在8月19日深圳GopherMeetup上的分享,以下为根据PPT进行的详细注解。介绍......
  • (转)Golang标准库——runtime
    原文:https://www.jianshu.com/p/c1b6de70c004runtimeruntime包提供和go运行时环境的互操作,如控制go程的函数。它也包括用于reflect包的低层次类型信息;参见》reflect报......
  • ME GO小车简单介绍
    怎么使用Mixly软件对MEGO小车初始化?MEGO小车怎么编写程序?怎么控制MEGO小车?……MEGO简介特点及功能:体积小巧集成多种传感器和执行器避障检测、自动巡线、灯光......
  • MongoDB使用
    启动MongoDB服务Windows在bin目录下运行:mongod--dbpath=..\data\dbE:\ProgramFiles\MongoDB\Server\6.0\bin>mongod--dbpath=..\data\db连接MongoDBWindows......