当前位置:网站首页>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
边栏推荐
- Building external modules
- JS date format conversion method
- 使用set_handler过滤掉特定的SystemC Wraning &Error Message
- Seckill system 03 - redis cache and distributed lock
- redis常识
- Want to ask, is there a discount for opening a securities account? Is it safe to open a mobile account?
- CPU 上下文切换的机制和类型 (CPU Context Switch)
- ABBIRB120工业机器人机械零点位置
- Learning summary on June 29, 2022
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
猜你喜欢

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%

Prepare for the Blue Bridge Cup Day10__ PWM control light brightness

Wonderful! MarkBERT

MQ-防止消息丢失及重复消费

Abbirb120 industrial robot mechanical zero position

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

Learning summary on June 30, 2022

使用set_handler过滤掉特定的SystemC Wraning &Error Message

Comment Cao définit la décimale de dimension

Neo4j Chinese developer monthly - issue 202206
随机推荐
Abbirb120 industrial robot mechanical zero position
指纹浏览器工作原理、使用场景以及重要性简单讲解
华为HMS Core携手超图为三维GIS注入新动能
Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
Acly and metabolic diseases
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%
对于mvvm和mvc的理解
Activity workflow engine
CPU 上下文切换的机制和类型 (CPU Context Switch)
Why must we move from Devops to bizdevops?
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
树莓派4B安装tensorflow2.0[通俗易懂]
How to understand the developed query statements
ACLY与代谢性疾病
Harbor webhook from principle to construction
Compile and debug net6 source code
8 best practices to protect your IAC security!
epoll介绍
自组织是管理者和成员的双向奔赴
Talk about the pessimistic strategy that triggers full GC?