当前位置: 代码网 > it编程>前端脚本>Golang > golang读取yaml配置文件的方法实现

golang读取yaml配置文件的方法实现

2024年11月26日 Golang 我要评论
viperviper是适用于go应用程序的完整配置解决方案。它被设计用于在应用程序中工作,并且可以处理所有类型的配置需求和格式。它支持以下特性:设置默认值从json、toml、yaml、hcl、env

viper

viper是适用于go应用程序的完整配置解决方案。它被设计用于在应用程序中工作,并且可以处理所有类型的配置需求和格式。它支持以下特性:

  • 设置默认值
  • 从json、toml、yaml、hcl、envfile和java properties 格式的配置文件读取配置信息
  • 实时监控和重新读取配置文件(可选)
  • 从环境变量中读取
  • 从远程配置系统(etcd或consul)读取并监控配置变化
  • 从命令行参数读取配置
  • 从buffer读取配置
  • 显式配置值

安装

go get github.com/spf13/viper

使用

在项目根目录创建config文件夹,在文件夹中创建local.yaml

servername: 'myserver'

mysql:
  name: 'mysql'

在main.go中读取配置文件的信息

package main

import (
	"log"

	"github.com/spf13/viper"
)

func main() {
	v := viper.new()

	// 设置文件名称
	v.setconfigfile("./config/local.yaml")

	if err := v.readinconfig(); err != nil {
		log.panicln("读取配置文件失败")
	}

	servername := v.get("servername")
	mysql := v.get("mysql.name")
	log.println(servername, mysql)
}

使用结构体映射yaml

修改配置文件

name: '服务名'
port: 8080

使用

package main

import (
	"log"

	"github.com/spf13/viper"
)

type serverconfig struct {
	name string `mapstructure:"name"`
	port int    `mapstructure:"port"`
}

func main() {

	cfg := serverconfig{}
	v := viper.new()

	// 设置文件名称
	v.setconfigfile("./config/local.yaml")

	if err := v.readinconfig(); err != nil {
		log.panicln("读取配置文件失败")
	}

	if err := v.unmarshal(&cfg); err != nil {
		log.panicln(err)
	}

	log.println(cfg)
}

输出

{服务名 8080}

结构体嵌套读取配置

配置文件

name: '服务名'
port: 8080
mysql: 
  host: '127.0.0.1'
  port: 3306

读取配置

package main

import (
	"log"

	"github.com/spf13/viper"
)

type serverconfig struct {
	name        string      `mapstructure:"name"`
	port        int         `mapstructure:"port"`
	mysqlconfig mysqlconfig `mapstructure:"mysql"` // 嵌套的配置
}

type mysqlconfig struct {
	host string `mapstructure:"host"`
	port int    `mapstructure:"port"`
}

func main() {

	cfg := serverconfig{}
	v := viper.new()

	// 设置文件名称
	v.setconfigfile("./config/local.yaml")

	if err := v.readinconfig(); err != nil {
		log.panicln("读取配置文件失败")
	}

	if err := v.unmarshal(&cfg); err != nil {
		log.panicln(err)
	}

	log.println(cfg)
}

输出

{服务名 8080 {127.0.0.1 3306}}

根据不同环境读取不同配置文件

在实际开发工作中,一般有不同的开发环境,会涉及到读取不同的配置文件

这会运用的golang的flag包

package main

import (
	"flag"
	"log"
)


func main() {

	env := flag.string("env", "local", "运行环境请输入local dev test prod")
	flag.parse()

	log.println("当前环境是" + *env)

}

运行

go run .\main.go -help

输出

-env string
运行环境请输入local dev test prod (default "local")

这样的话运行

go run .\main.go -env dev

输出

当前环境是dev

利用flag读取不同环境的配置文件

在项目更目录新建config文件夹

并创建4个配置文件 local.yaml dev.yaml test.yaml prod.yaml

其中dev.yaml中

name: 'dev服务名'
port: 8080
mysql: 
  host: '127.0.0.1'
  port: 3306

在main.go中

package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/spf13/viper"
)

type serverconfig struct {
	name        string      `mapstructure:"name"`
	port        int         `mapstructure:"port"`
	mysqlconfig mysqlconfig `mapstructure:"mysql"` // 嵌套的配置
}

type mysqlconfig struct {
	host string `mapstructure:"host"`
	port int    `mapstructure:"port"`
}

func main() {

	env := flag.string("env", "local", "运行环境请输入local dev test prod")
	flag.parse()

	log.println("当前环境是" + *env)

	cfg := serverconfig{}
	v := viper.new()
	var path string = fmt.sprintf("./config/%s.yaml", *env)
	// 设置文件名称
	v.setconfigfile(path)

	if err := v.readinconfig(); err != nil {
		log.panicln("读取配置文件失败")
	}

	if err := v.unmarshal(&cfg); err != nil {
		log.panicln(err)
	}

	log.println(cfg)
}

运行

go run .\main.go -env dev

输出

当前环境是dev
{dev服务名 8080 {127.0.0.1 3306}}

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com