当前位置:网站首页>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
边栏推荐
- Summary of JFrame knowledge points 1
- 微信小程序开发 – 用户授权登陆「建议收藏」
- 二叉堆(一) - 原理与C实现
- Vscode shortcut key (the most complete) [easy to understand]
- 强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
- redis中value/SortedSet
- Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
- 构建外部模块(Building External Modules)
- About keil compiler, "file has been changed outside the editor, reload?" Solutions for
- Impressive bug summary (continuously updated)
猜你喜欢
![[MCU] [nixie tube] nixie tube display](/img/5e/9e14302b4e4f5e03601392ac90479d.png)
[MCU] [nixie tube] nixie tube display

Emotion analysis based on IMDB comment data set

Exposure: a white box photo post processing framework reading notes

redis配置环境变量

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%

Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode

Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)

Adjacency matrix undirected graph (I) - basic concepts and C language

用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义

Neo4j Chinese developer monthly - issue 202206
随机推荐
Personnaliser le plug - in GRPC
Skip the test cases to be executed in the unittest framework
Dataset partitioning script for classification tasks
solo 可以通过 IPV6 访问吗?
Software project management 9.2 Software project configuration management process
Question: what professional qualities should test engineers have?
Epoll introduction
Learning summary on June 28, 2022
伸展树(一) - 概念和C实现
Why must we move from Devops to bizdevops?
今天开户今天能买股票吗?在线开户是很安全么?
Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
Redis' attack tactics
Nordic nrf52832 flash download M4 error
Force button homepage introduction animation
ABBIRB120工业机器人机械零点位置
Abbirb120 industrial robot mechanical zero position
Summary of JFrame knowledge points 1
超详细黑苹果安装图文教程送EFI配置合集及系统
Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ