2 回答

TA貢獻1830條經驗 獲得超3個贊
我會建議你按照命令行上的Go文檔的建議使用標志-X
-X importpath.name=value
Set the value of the string variable in importpath named name to value.
This is only effective if the variable is declared in the source code either uninitialized or initialized to a constant string expression. -X will not work if the initializer makes a function call or refers to other variables.
Note that before Go 1.5 this option took two separate arguments.
這樣,您就可以調用引用它的任何文件的位置。.env
例如go build -ldflags="-X 'package_path.variable_name=new_value'"
那是
go build -ldflags "-X 'my/main/config.Version=v1.0.0'" -o $(MY_BIN) $(MY_SRC)

TA貢獻1815條經驗 獲得超6個贊
在構建階段對環境進行硬編碼對我來說似乎很奇怪。您不想為每個env構建差異映像,這是浪費。
模塊文檔建議了更好的方法:
現有 env 優先于稍后加載的 env。
管理多個環境(即開發、測試、生產)的約定是創建一個名為 {YOURAPP}_ENV的 env,并按以下順序加載 envs:
env := os.Getenv("FOO_ENV")
if "" == env {
env = "development"
}
godotenv.Load(".env." + env + ".local")
if "test" != env {
godotenv.Load(".env.local")
}
godotenv.Load(".env." + env)
godotenv.Load() // The Original .env
- 2 回答
- 0 關注
- 110 瀏覽
添加回答
舉報