go项目中环境变量的配置
作者:水痕001 时间:2024-02-12 12:17:55
一、为什么要配置文件
1、容易编辑,配置比较集中,方便修改,在大业务量的系统里面,通过配置会方便后人理解整个系统的架构
2、做到业务代码和环境解耦,环境配置变量不侵入到代码层面
3、不同的环境切换比较方便
二、在go中使用配置文件
1、viper网站地址
2、本地安装
go get github.com/spf13/viper
3、简单的读取配置文件
package main
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
v := viper.New()
v.SetConfigFile("config.yaml")
if err := v.ReadInConfig(); err != nil {
fmt.Println("读取配置文件失败", err)
}
fmt.Println(v.Get("name"))
}
4、在文件的同级下创建一个config.yaml的文件
name: "test"
5、使用go run 文件名运行文件,这样直接运行会报错找不到文件
6、建议配置后可以使用右键运行项目
package main
import (
"fmt"
"github.com/spf13/viper"
"os"
"path"
)
func main() {
// 获取项目目录
workDir, _ := os.Getwd()
v := viper.New()
// 直接拼接文件目录读取
v.SetConfigFile(path.Join(workDir, "test/config.yaml"))
if err := v.ReadInConfig(); err != nil {
fmt.Println("读取配置文件失败", err)
}
fmt.Println(v.Get("name"))
}
三、使用结构体的方式读取
1、简单的使用
package main
import (
"fmt"
"github.com/spf13/viper"
"os"
"path"
)
type ServerConfig struct {
Name string `mapstructure:"name"`
}
func main() {
// 获取项目目录
workDir, _ := os.Getwd()
v := viper.New()
v.SetConfigFile(path.Join(workDir, "test/config.yaml"))
if err := v.ReadInConfig(); err != nil {
fmt.Println("读取配置文件失败", err)
}
serverConfig := ServerConfig{}
if err := v.Unmarshal(&serverConfig); err != nil {
fmt.Println("解析结构体失败", err)
}
fmt.Println(serverConfig.Name)
}
2、结构体的嵌套,很常见的是mysql数据库的配置
// 定义mysql的
type MySQLConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}
type ServerConfig struct {
Name string `mapstructure:"name"`
// 嵌套使用,注意这个地方要和yaml中一样
MySqlConfig MySQLConfig `mapstructure:"mysql"`
}
// 直接使用
fmt.Println(serverConfig.MySqlConfig)
四、区分开发环境和生产环境
1、在mac电脑上配置环境变量,在.bash_profile中导出一个环境变量,注意要关闭goland重新打开
# vim .bash_profile
export IS_DEV= true
source .bash_profile
2、定义方法直接获取环境变量
func GetEnvInfo(env string) bool {
viper.AutomaticEnv()
return viper.GetBool(env)
}
func main() {
fmt.Println(GetEnvInfo("IS_DEV"))
}
3、在目录下创建一个application.dev.yml和application.prod.yml的文件,分别存放不同环境下的配置
4、使用环境变量来读取配置完整代码
func GetEnvInfo1(env string) bool {
viper.AutomaticEnv()
return viper.GetBool(env)
}
// 定义mysql的
type MySQLConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}
type ServerConfig struct {
Name string `mapstructure:"name"`
// 嵌套使用,注意这个地方要和yaml中一样
MySqlConfig MySQLConfig `mapstructure:"mysql"`
}
func main() {
// 获取项目目录
workDir, _ := os.Getwd()
isDev := GetEnvInfo1("IS_DEV")
configFileName := path.Join(workDir, "11.config/application.prod.yml")
if isDev {
configFileName = path.Join(workDir, "11.config/application.dev.yml")
}
v := viper.New()
v.SetConfigFile(configFileName)
if err := v.ReadInConfig(); err != nil {
fmt.Println("读取配置文件失败", err)
}
serverConfig := ServerConfig{}
if err := v.Unmarshal(&serverConfig); err != nil {
fmt.Println("解析结构体失败", err)
}
fmt.Println(serverConfig.MySqlConfig)
}
五、在gin中集成配置文件
1、在和main.go同级的目录下创建两个xx.yml的文件
2、目录下创建一个config、global、initialize的文件夹
3、在config/config.go的文件中书写使用结构体读取配置的
package config
// 定义mysql的
type MySQLConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}
type ServerConfig struct {
Name string `mapstructure:"name"`
// 嵌套使用,注意这个地方要和yaml中一样
MySqlConfig MySQLConfig `mapstructure:"mysql"`
}
4、在global/global.go文件中定义全局变量(可能在全项目中某一个文件中需要获取配置)
var (
ServerConfig *config.ServerConfig = &config.ServerConfig{}
)
5、在initialize/config目录初始化配置
func GetEnvInfo(env string) bool {
viper.AutomaticEnv()
return viper.GetBool(env)
}
func InitConfig() {
workDir, _ := os.Getwd()
isDev := GetEnvInfo("IS_DEV")
configFileName := path.Join(workDir, "11.config/application.prod.yml")
if isDev {
configFileName = path.Join(workDir, "11.config/application.dev.yml")
}
v := viper.New()
//文件的路径如何设置
v.SetConfigFile(configFileName)
if err := v.ReadInConfig(); err != nil {
panic(err)
}
err := v.Unmarshal(&global.ServerConfig)
if err != nil {
fmt.Println("读取配置失败")
}
fmt.Println(&global.ServerConfig)
}
6、在mian.go中初始化配置文件并且打印出信息
func main() {
// 初始化配置
initialize.InitConfig()
fmt.Println(global.ServerConfig.MySqlConfig)
}
来源:https://juejin.cn/post/6983290445577060382
标签:go,环境变量,配置
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP分页类(支持多风格变换)
2011-04-08 10:39:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125112048960.jpg)
详解python使用turtle库来画一朵花
2021-08-30 14:58:49
![](https://img.aspxhome.com/file/2023/7/72857_0s.png)
Go逃逸分析示例详解
2024-04-25 15:05:22
![](https://img.aspxhome.com/file/2023/2/134232_0s.jpg)
python实现Virginia无密钥解密
2023-07-25 10:56:49
基于Python实现中秋佳节月饼抢购脚本
2023-02-23 12:48:39
![](https://img.aspxhome.com/file/2023/8/94068_0s.webp)
Python 爬虫批量爬取网页图片保存到本地的实现代码
2021-06-23 02:12:34
![](https://img.aspxhome.com/file/2023/5/124175_0s.png)
在windows下Python打印彩色字体的方法
2023-05-19 07:03:32
语言编程花絮内建构建顺序示例详解
2023-11-04 09:42:12
Python字典遍历的陷阱
2022-11-16 21:59:11
Python中断多重循环的几种方式详解
2022-05-09 21:44:04
Python去除PDF水印的实现示例
2022-08-13 19:26:45
![](https://img.aspxhome.com/file/2023/3/100803_0s.png)
mysql 开启慢查询 如何打开mysql的慢查询日志记录
2024-01-20 20:48:35
centos 安装python3.6环境并配置虚拟环境的详细教程
2021-06-10 12:24:49
keras Lambda自定义层实现数据的切片方式,Lambda传参数
2021-03-18 09:16:20
![](https://img.aspxhome.com/file/2023/1/133461_0s.jpg)
vue3 $attrs和inheritAttrs的用法说明
2024-04-26 17:39:08
![](https://img.aspxhome.com/file/2023/3/133163_0s.png)
《写给大家看的设计书》阅读笔记之对齐原则
2009-07-09 16:32:00
![](https://img.aspxhome.com/file/UploadPic/20097/9/dq-25s.jpg)
简单获取键盘的KeyCode
2008-04-18 12:37:00
MySQL时间格式化date_format使用语法
2024-01-23 07:31:36
一些让Python代码简洁的实用技巧总结
2022-02-06 11:03:25
Django点赞的实现示例
2022-08-24 20:13:40
![](https://img.aspxhome.com/file/2023/3/118103_0s.png)