英文:
How to override configuration?
问题
我们有针对每个部署环境的配置文件,如下所示:
configs/.dev.env
deploy_env=dev
configs/.env
deploy_env=local
main.go
package main
import (
"fmt"
"os"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load("./configs/.env") // 加载
if err != nil {
fmt.Println("无法加载")
}
err = godotenv.Load("./configs/.dev.env") // 期望覆盖配置
if err != nil {
fmt.Println("无法加载")
}
fmt.Println(os.Getenv("deploy_env")) // 输出为:local
// 期望输出:dev
}
如何覆盖配置?期望将 deploy_env
设置为 dev
?
英文:
We have configuration file per deploy environment, as shown in below code:
configs/.dev.env
deploy_env=dev
configs/.env
deploy_env=local
main.go
package main
import (
"fmt"
"os"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load("./configs/.env") // load
if err != nil {
fmt.Println("unable to load")
}
err = godotenv.Load("./configs/.dev.env") // expecting to override configuration
if err != nil {
fmt.Println("unable to load")
}
fmt.Println(os.Getenv("deploy_env")) // Got output as: local
// Expecting output: dev
}
How to override configuration? expecting deploy_env
to set as dev
?
答案1
得分: 2
使用Overload
代替Load
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论