Go YAML unmarshal 不会将所有对象结构解码为通用接口。任何想法为什么?
Go YAML unmarshal doesn't decode all the object structure to a generic interface. Any ideas why?
为什么以下 Go
程序不起作用? Unmarshal 之后地图是空的。我可以做任何更改来正确反序列化吗?或者我应该最终使用 yaml.Node
?
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
func main() {
empYaml := `
employees:
- id: 11
name: Irshad
department: IT
designation: Product Manager
address:
city: Mumba
state: Maharashtra
country: India
`
var result map[string]interface{}
yaml.Unmarshal([]byte(empYaml), &result)
fmt.Println(result)
}
当我检查调用 yaml.Unmarshal
的错误时,我看到错误:
2009/11/10 23:00:00 yaml: line 2: found character that cannot start any token
看起来这可能是由于 YAML forbids tabs.
后内容中的制表符所致
从 YAML 内容中删除所有制表符,您应该处于良好状态。我得到以下输出:
map[employees:[map[address:map[city:Mumba country:India state:Maharashtra] department:IT designation:Product Manager id:11 name:Irshad]]]
为什么以下 Go
程序不起作用? Unmarshal 之后地图是空的。我可以做任何更改来正确反序列化吗?或者我应该最终使用 yaml.Node
?
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
func main() {
empYaml := `
employees:
- id: 11
name: Irshad
department: IT
designation: Product Manager
address:
city: Mumba
state: Maharashtra
country: India
`
var result map[string]interface{}
yaml.Unmarshal([]byte(empYaml), &result)
fmt.Println(result)
}
当我检查调用 yaml.Unmarshal
的错误时,我看到错误:
2009/11/10 23:00:00 yaml: line 2: found character that cannot start any token
看起来这可能是由于 YAML forbids tabs.
后内容中的制表符所致从 YAML 内容中删除所有制表符,您应该处于良好状态。我得到以下输出:
map[employees:[map[address:map[city:Mumba country:India state:Maharashtra] department:IT designation:Product Manager id:11 name:Irshad]]]