当前位置:网站首页>Golang introduces the implementation method of the corresponding configuration file according to the parameters
Golang introduces the implementation method of the corresponding configuration file according to the parameters
2022-07-01 11:53:00 【Master Guo, who sells baked cold noodles】
1) config Catalog establish dev.env and prod.env
env File example
dev.env
#gin
GIN_DEBUG=true
GIN_PORT=9000
prod.env
#gin
GIN_DEBUG=false
GIN_PORT=9527
2) Use godotenv Import the corresponding file
Without this package, you need to introduce
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() {
// The default is dev Environmental Science
flag.StringVar(&envArgs, "env", "dev", " Running environment ")
}
func main() {
// It can only be used after parsing
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)
}
Command line execution
#prod Environmental Science
go run main.go -env=prod
#dev Environmental Science If env The default value is dev
go run main.go -env=dev
边栏推荐
- Neo4j Chinese developer monthly - issue 202206
- Compile and debug net6 source code
- ES6 promise Usage Summary
- ABBIRB120工业机器人机械零点位置
- Why must we move from Devops to bizdevops?
- Raspberry pie 4B installation tensorflow2.0[easy to understand]
- 构建外部模块(Building External Modules)
- 超详细黑苹果安装图文教程送EFI配置合集及系统
- 谈思生物直播—GENOVIS张洪妍抗体特异性酶切技术助力抗体药物结构表征
- 自定义 grpc 插件
猜你喜欢
深入理解 grpc part1
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
About keil compiler, "file has been changed outside the editor, reload?" Solutions for
使用set_handler过滤掉特定的SystemC Wraning &Error Message
Prepare for the Blue Bridge Cup Day10__ PWM control light brightness
Emotion analysis based on IMDB comment data set
如何看懂开发的查询语句
MQ prevent message loss and repeated consumption
2022/6/29学习总结
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%
随机推荐
Seckill system 03 - redis cache and distributed lock
Neo4j 中文开发者月刊 - 202206期
Kafuka learning path (I) Kafuka installation and simple use
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
CPI tutorial - asynchronous interface creation and use
Theoretical basis of graph
[Maui] add click events for label, image and other controls
JS date format conversion method
ABBIRB120工业机器人机械零点位置
CAD如何设置标注小数位
C summary of knowledge points 3
陈珙:微服务,它还那么纯粹吗?
妙啊!MarkBERT
The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
Exploration and practice of inress in kubernetes
二叉堆(一) - 原理与C实现
博途V15添加GSD文件
证券账户销户后果 开户安全吗
今天开户今天能买股票吗?在线开户是很安全么?
小米手机解BL锁教程