我正在使用 Google App Engine 并有一個 app.yaml 檔案,如下所示:
runtime: go115
env_variables:
INSTANCE_CONNECTION_NAME: secret:northamerica-northeast1:special
DB_USER: db_username
DB_PASS: p@ssword
DB_NAME: superduper
使用這個Github作為參考,我用它來讀取這些值:
dbUser = mustGetenv("DB_USER")
我執行 gcloud 應用程式部署,然后使用 set 命令檢查變數。我在那里看不到變數,同樣,當我的程式運行時,它找不到變數。
我似乎正在做示例所做的一切,但我的程式沒有找到我的變數。yaml 檔案有什么特定的格式嗎?在嘗試讀取變數之前,我還需要做什么其他步驟嗎?
uj5u.com熱心網友回復:
您可以使用os.Getenv.
dbUser = os.Getenv("DB_USER")
你必須參考你的價值觀。
env_variables:
INSTANCE_CONNECTION_NAME: 'secret:northamerica-northeast1:special'
DB_USER: 'db_username'
DB_PASS: 'p@ssword'
DB_NAME: 'superduper'
uj5u.com熱心網友回復:
您可以嘗試使用毒蛇https://github.com/spf13/viper
func Init() {
// Set the file name of the configurations file
viper.SetConfigName("app")
// Set the path to look for the configurations file
viper.AddConfigPath("./config")
// Enable VIPER to read Environment Variables
viper.AutomaticEnv()
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file, %s", err)
}
}
在您的 main 中添加此初始化,然后您可以在代碼中的任何位置使用以下內容進行訪問
viper.GetString("env_variables.INSTANCE_CONNECTION_NAME")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358274.html
上一篇:我的函式應該將通道作為輸入嗎?
