【GO】解析yaml文件

技术网红
• 阅读 3028

参考资料

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

1. 书写dev_custom.yml文件内容

mySql:
  dns: "sqlserver://sa:123456@DESKTOP-HMTA87I:1433?database=wzz"

sqlServer:
  dns: "me:123abc@tcp(10.0.0.0:3306)/wzz_db?charset=utf8"

2. 解析dev_custom.yml文件内容

package config

import (
    "github.com/mitchellh/mapstructure"
    "github.com/spf13/viper"
    "log"
    "os"
)

// CustomT CustomT
type Mysql struct {
    DNS string `yaml:"dns"`
}

type SqlServer struct {
    DNS string `yaml:"dns"`
}

// CustomT CustomT
type CustomT struct {
    Mysql     Mysql     `yaml:"mySql"`
    SqlServer SqlServer `yaml:"sqlServer"`
}

// Custom Custom
var Custom CustomT

// ReadConfig ReadConfig for custom
func ReadConfig(configName string, configPath string, configType string) *viper.Viper {
    v := viper.New()
    v.SetConfigName(configName)
    v.AddConfigPath(configPath)
    v.SetConfigType(configType)
    err := v.ReadInConfig()
    if err != nil {
        return nil
    }

    res := v.AllKeys()
    log.Println("res=", res)

    return v
}

// InitConfig InitConfig
func InitConfig() (err error) {
    path, err := os.Getwd()
    if err != nil {
        return err
    }

    v := ReadConfig("dev_custom", path, "yml")
    md := mapstructure.Metadata{}
    err = v.Unmarshal(&Custom, func(config *mapstructure.DecoderConfig) {
        config.TagName = "yaml"
        config.Metadata = &md
    })

    return err
}

3. 测试

package config

import (
    "testing"
)

func TestConfig(t *testing.T) {
    err := InitConfig()
    if err != nil {
        t.Fatal(err)
    }
    t.Log("mysqlDNS=", Custom.Mysql.DNS)
    t.Log("sqlserverDNS=", Custom.SqlServer.DNS)
}
点赞
收藏
评论区
推荐文章
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Easter79 Easter79
4年前
springcloud的配置文件的读取顺序
SpringBoot默认支持properties和YAML两种格式的配置文件。前者格式简单,但是只支持键值对。如果需要表达列表,最好使用YAML格式。SpringBoot支持自动加载约定名称的配置文件,例如application.yml。如果是自定义名称的配置文件,就要另找方法了。可惜的是,不像前者有@PropertySource这样方便的加载方式,
顺心 顺心
5年前
Flutter如何引用第三方库并使用
Flutter官网点击访问(https://link.jianshu.com/?thttps%3A%2F%2Fflutter.io)如何引用并安装第三方库pubspec.yaml管理第三方库在pubspec.yaml中添加第三方库名称及版本号。例如添加第三方库english_wordsdependencies:flut
Stella981 Stella981
4年前
SpringBoot集成mybatis以及自动化测试代码实现
Mybatis和logback的应用配置1、在module的pom.xml文件中,加载springboot和swagger、lombok、fastjson、mysql、mybatis包2、在resources中添加配置:配置文件有两种,一种是properties,另一种是yaml,这里使用yamlyaml配
Stella981 Stella981
4年前
HyperLedger Fabric 错误记录
错误1:在阿里云机器上运行环境:signalSIGSEGV:segmentationviolationcode0x1addr0x63pc0x7fecd68ca259需要修改所有相关docker容器yaml文件,在environment中加入image:hyperledger/fabric
Stella981 Stella981
4年前
SpringBoot系列之@PropertySource读取yaml文件
SpringBoot系列之@PropertySource支持yaml文件读取最近在做实验,想通过@PropertySource注解读取配置文件的属性,进行映射,习惯上用properties都是测试没问题的,偶然换成yaml文件,发现都读取不到属性值因为yaml语法很简洁,比较喜欢写yaml配置文件,很显然,@PropertySource默认不支持ya
Easter79 Easter79
4年前
SpringBoot集成mybatis以及自动化测试代码实现
Mybatis和logback的应用配置1、在module的pom.xml文件中,加载springboot和swagger、lombok、fastjson、mysql、mybatis包2、在resources中添加配置:配置文件有两种,一种是properties,另一种是yaml,这里使用yamlyaml配
Easter79 Easter79
4年前
SpringBoot系列之@PropertySource读取yaml文件
SpringBoot系列之@PropertySource支持yaml文件读取最近在做实验,想通过@PropertySource注解读取配置文件的属性,进行映射,习惯上用properties都是测试没问题的,偶然换成yaml文件,发现都读取不到属性值因为yaml语法很简洁,比较喜欢写yaml配置文件,很显然,@PropertySource默认不支持ya
Stella981 Stella981
4年前
Golang 入门系列(九) 如何读取YAML,JSON,INI等配置文件
实际项目中,读取相关的系统配置文件是很常见的事情。今天就来说一说,Golang是如何读取YAML,JSON,INI等配置文件的。1\.json使用JSON应该比较熟悉,它是一种轻量级的数据交换格式。层次结构简洁清晰,易于阅读和编写,同时也易于机器解析和生成。  1.创建conf.json:{
Stella981 Stella981
4年前
Kubernetes1.15 部署 coredns
coredns.yaml文件如下所示__MACHINE_GENERATED_WARNING__apiVersion:v1kind:ServiceAccountmetadata:name:corednsnamespace:kubesystemlabels
技术网红
技术网红
Lv1
往事与你兵戎相见,岁月对你张牙舞爪,你选择屈从
文章
4
粉丝
0
获赞
0