1 回答

TA貢獻2016條經驗 獲得超9個贊
您可以使用 viper 讀取您的 .yaml 配置文件。簡單代碼示例:
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
readYaml()
}
func readYaml() {
viper.SetConfigName("test") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("./Examples/readyaml") // path to look for the config file in
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
fmt.Println(viper.AllKeys())
for _, i := range viper.AllKeys() {
fmt.Println(i, viper.Get(i))
}
}
輸出:
[initsteps buildsteps runprocess]
initsteps [pip install --upgrade pip python3 --version]
buildsteps [pip install .]
runprocess [python3 test.py]
- 1 回答
- 0 關注
- 139 瀏覽
添加回答
舉報