我有以下 YML 文件test.ymluser_name:Agent1org_info: first:hello second:world我嘗試test.yml使用以下 golang 代碼解組package mainimport ( "log" "io/ioutil" "gopkg.in/yaml.v2")func main() { content, _ := ioutil.ReadFile("./test.yml") var t interface{} yaml.Unmarshal(content, &t) log.Println(t)}但是log.Println(t)給出了nil。我將test.yml文件縮減為:user_name:Agent1org_info:但log.Println(t)仍然給nil。我如何使用 golang 解組具有不可預測模式的 yaml 文件,其中的字段沒有值或導致嵌套和縮進子字段的字段?或者我應該使用另一個 golang yaml 解析器嗎?
1 回答

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
yaml.Unmarshal()返回錯誤:
yaml:第 2 行:此上下文中不允許映射值
永遠不要跳過錯誤檢查:
var t interface{}
err = yaml.Unmarshal(content, &t)
if err != nil {
log.Fatal(err)
}
在冒號后添加缺失的空格,使它們成為 YAML 值指示符:
user_name: Agent1
org_info:
first: hello
second: world
- 1 回答
- 0 關注
- 241 瀏覽
添加回答
舉報
0/150
提交
取消