当前位置:网站首页>Golang根据参数引入相应配置文件的实现方式
Golang根据参数引入相应配置文件的实现方式
2022-07-01 11:53:00 【卖烤冷面的郭师傅】
1) config目录 创建dev.env和prod.env
env文件示例
dev.env
#gin
GIN_DEBUG=true
GIN_PORT=9000
prod.env
#gin
GIN_DEBUG=false
GIN_PORT=9527
2) 使用godotenv引入对应的文件
没有这个包的话需要先引入
go get github.com/joho/godotenv
package main
import (
"flag"
"log"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
var envArgs string
func init() {
// 默认为dev环境
flag.StringVar(&envArgs, "env", "dev", "运行环境")
}
func main() {
// 需要先解析才能使用
flag.Parse()
err := godotenv.Load("./config/" + string(envArgs) + ".env")
if err != nil {
log.Fatal("Error loading .env file")
}
ginPort := os.GetEnv("GIN_PORT")
debugFlag := os.GetEnv("GIN_DEBUG")
if debugFlag == "true" {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
r.Run(":" + ginPort)
}
命令行执行
#prod环境
go run main.go -env=prod
#dev环境 如果env不传默认为dev
go run main.go -env=dev
边栏推荐
猜你喜欢
![[classic example] classic list questions @ list](/img/d8/a259e5f9d08eacbef31254d1bc3304.jpg)
[classic example] classic list questions @ list

Software project management 9.2 Software project configuration management process

华为HMS Core携手超图为三维GIS注入新动能

Oneconnect plans to be listed in Hong Kong on July 4: a loss of nearly 3 billion in two years, with a market capitalization evaporation of more than 90%

uniapp 使用 uni-upgrade-center

Abbirb120 industrial robot mechanical zero position

Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ

Learning summary on June 30, 2022

图的理论基础

Redis configuration environment variables
随机推荐
2022/6/29学习总结
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Skip the test cases to be executed in the unittest framework
自组织是管理者和成员的双向奔赴
印象深刻的bug汇总(持续更新)
使用set_handler过滤掉特定的SystemC Wraning &Error Message
openinstall:微信小程序跳转H5配置业务域名教程
[Maui] add click events for label, image and other controls
S7-1500PLC仿真
Can I open an account today and buy stocks today? Is it safe to open an account online?
epoll介绍
Summary of JFrame knowledge points 2
CPU 上下文切换的机制和类型 (CPU Context Switch)
Mingchuang plans to be listed on July 13: the highest issue price is HK $22.1, and the net profit in a single quarter decreases by 19%
[classic example] classic list questions @ list
Epoll introduction
redis中value/SortedSet
Botu V15 add GSD file
Nordic nrf52832 flash 下载M4错误
Test case writing specification in unittest framework and how to run test cases