当前位置:网站首页>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
边栏推荐
- sshd_config 中 PermitRootLogin 的探讨
- Impressive bug summary (continuously updated)
- redis中value/SortedSet
- Can I open an account today and buy stocks today? Is it safe to open an account online?
- Test case writing specification in unittest framework and how to run test cases
- Redis common sense
- [buuctf.reverse] 144_ [xman2018 qualifying]easyvm
- Neo4j Chinese developer monthly - issue 202206
- Extended tree (I) - concept and C implementation
- Exposure: a white box photo post processing framework reading notes
猜你喜欢
陈珙:微服务,它还那么纯粹吗?
CPI tutorial - asynchronous interface creation and use
【单片机】【数码管】数码管显示
Tianrunyun, invested by Tian Suning, was listed: its market value was 2.2 billion Hong Kong, and its first year profit decreased by 75%
Istio、eBPF 和 RSocket Broker:深入研究服务网格
邻接矩阵无向图(一) - 基本概念与C语言
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
Exposure: a white box photo post processing framework reading notes
redis配置环境变量
ABBIRB120工业机器人机械零点位置
随机推荐
内核同步机制
JS date format conversion method
IPlImage的width和widthStep
CPI tutorial - asynchronous interface creation and use
redis中value/set
ACLY与代谢性疾病
力扣首页简介动画
Test case writing specification in unittest framework and how to run test cases
证券账户销户后果 开户安全吗
solo 可以通过 IPV6 访问吗?
Jd.com renewed its cooperation with Tencent: issuing class A shares to Tencent with a maximum value of US $220million
Custom grpc plug-in
C#依赖注入(直白明了)讲解 一看就会系列
2022/6/28学习总结
activity工作流引擎
Mechanism and type of CPU context switch
Redis configuration environment variables
Kafuka learning path (I) Kafuka installation and simple use
Getting started with Paxos
Why must we move from Devops to bizdevops?