通过yaml文件为golang应用程序配置环境设置的方法
来源:stackoverflow
时间:2024-02-08 17:15:23 483浏览 收藏
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《通过yaml文件为golang应用程序配置环境设置的方法》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
我正在尝试设置一个配置结构以通过我的应用程序使用。
目前,我加载一个 yaml 文件并在我的配置结构中对其进行解码。
config.yml
database_url: postgres://postgres:@localhost:5432/database_dev
config.go
import (
"os"
"gopkg.in/yaml.v2"
)
type appconfig struct {
databaseurl string `yaml:"database_url"`
}
func loadconfig() *appconfig {
appconfig := &appconfig{}
file, _ := os.open("config.yml")
defer f.close()
decoder := yaml.newdecoder(file)
decoder.decode(config)
return appconfig
}
它工作得很好,但现在我需要根据环境(测试、本地、生产等)设置不同的配置。
我认为我可以使用嵌套的 yaml 文件来声明环境变量。
config.yml
dev: database_url: postgres://postgres:@localhost:5432/database_dev test: database_url: postgres://postgres:@localhost:5432/database_test
我想在我的 loadconfig 函数中接收环境作为参数,并获得正确的配置。
但我不知道该怎么做。
config.go
type configFile struct {
Dev struct { AppConfig }`yaml:"dev"`
Test struct { AppConfig }`yaml:"test"`
}
func LoadConfig(env string) *AppConfig {
appConfig := &AppConfig{}
configFile := &configFile{}
file, _ := os.Open("config.yml")
defer f.Close()
decoder := yaml.NewDecoder(file)
decoder.Decode(configFile)
// How to get the correct struct here ?
// config = configFile["env"]
// It doesn't works
// invalid operation: cannot index configFile (variable of type *configFile)
return appConfig
}
欢迎任何建议。
正确答案
如果环境列表是任意的,那么您不需要 struct 作为顶层;你想要一个 map[string]appconfig。看起来像这样:
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v2"
)
type (
appconfig struct {
databaseurl string `yaml:"database_url"`
}
configfile map[string]*appconfig
)
func loadconfig(env string) (*appconfig, error) {
configfile := configfile{}
file, _ := os.open("config.yml")
defer file.close()
decoder := yaml.newdecoder(file)
// always check for errors!
if err := decoder.decode(&configfile); err != nil {
return nil, err
}
appconfig, ok := configfile[env]
if !ok {
return nil, fmt.errorf("no such environment: %s", env)
}
return appconfig, nil
}
func main() {
appconfig, err := loadconfig(os.args[1])
if err != nil {
panic(err)
}
fmt.printf("config: %+v\n", appconfig)
}
假设我们有您问题中的 config.yml,我们可以在不同的环境下运行上面的示例并查看所需的输出:
$ ./example test
config: &{DatabaseUrl:postgres://postgres:@localhost:5432/database_test}
$ ./example dev
config: &{DatabaseUrl:postgres://postgres:@localhost:5432/database_dev}我认为一种方法是我们可以针对不同的环境使用不同的配置文件。例如,在开发环境中,我们有 config_dev.yaml,在生产环境中,我们有 config_prod.yaml。
然后我们在引导脚本中指定环境变量。例如,我们通过 export myenv=dev && ./my_app 运行应用程序。在代码中,我们检查 myenv 来决定使用哪个配置文件。在本例中,我们发现 myenv 是 dev,因此我们使用 config_dev.yaml。
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习