当前位置:网站首页>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
边栏推荐
- TMUX usage
- 分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
- Summary of JFrame knowledge points 1
- 2022/6/30学习总结
- Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
- Redis common sense
- Can I open a securities account anywhere? Is it safe to open an account
- Test case writing specification in unittest framework and how to run test cases
- Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
- 指纹浏览器工作原理、使用场景以及重要性简单讲解
猜你喜欢

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

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

Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project

Istio, ebpf and rsocket Broker: in depth study of service grid

About keil compiler, "file has been changed outside the editor, reload?" Solutions for

Exposure: a white box photo post processing framework reading notes

S7-1500PLC仿真

C summary of knowledge points 1

Abbirb120 industrial robot mechanical zero position

Emotion analysis based on IMDB comment data set
随机推荐
谈思生物直播—GENOVIS张洪妍抗体特异性酶切技术助力抗体药物结构表征
[classic example] classic list questions @ list
Prepare for the Blue Bridge Cup Day10__ PWM control light brightness
Wonderful! MarkBERT
Chen Gong: Micro service, is it still so pure?
Xiaomi mobile phone unlocking BL tutorial
耐克如何常年霸榜第一名?最新財報答案來了
Ultra detailed black apple installation graphic tutorial sent to EFI configuration collection and system
S7-1500plc simulation
二叉堆(一) - 原理与C实现
How to understand the developed query statements
Unity XLua 协程封装
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
On recursion and Fibonacci sequence
Can I open an account today and buy stocks today? Is it safe to open an account online?
Learning summary on June 29, 2022
ES6 Promise用法小结
[buuctf.reverse] 144_ [xman2018 qualifying]easyvm
Are the consequences of securities account cancellation safe
深入理解 grpc part1