无法在Go语言中循环遍历yaml.MapItem.Value。

huangapple go评论132阅读模式
英文:

Cannot loop through yaml.MapItem.Value in go

问题

我正在尝试解析一个 YAML 文件,并保持表格处理的顺序(由于外键问题)。

fixtures.yaml

  1. table1:
  2. - id: 1
  3. field1: "some value"
  4. field2: "some value"
  5. table2:
  6. - id: 1
  7. field1: "some value"
  8. field2: "some value"
  9. ...更多数据...

main.go

  1. yamlContent, err := ioutil.ReadFile("fixtures.yaml")
  2. yamlOutput := yaml.MapSlice{}
  3. err := yaml.Unmarshal(yamlContent, &yamlOutput)
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. for _, tableData := range yamlOutput {
  8. // 表格名称
  9. fmt.Println(tableData.Key)
  10. // 表格数据
  11. fmt.Println(tableData.Value)
  12. // 这里出错
  13. for _, row := range tableData.Value {
  14. fmt.Println(row)
  15. }
  16. }

tableData.Value 的值看起来像这样:

  1. [[{id 1} {field1 some value} {field2 some value} {field3 some value} {field4 some value} {field5 some value}]]

问题是我无法遍历 tableData.Value。每当我尝试这样做时,都会出现错误:

  1. cannot range over tableData.Value (type interface {})

但是,当我使用 reflect.TypeOf(tableData.Value) 时,我得到的是 []interface {}

我应该怎么做才能循环遍历每一行,然后再遍历每一个键值对?我在 Go 中还是新手,所以不太确定接下来该怎么做。

英文:

I'm trying to parse a yaml file while keeping the order (due to foreign key issues) of tables being processed.

fixtures.yaml

  1. table1:
  2. -id: 1
  3. field1: "some value"
  4. field2: "some value"
  5. table2:
  6. -id: 1
  7. field1: "some value"
  8. field2: "some value"
  9. ...some more data...

main.go

  1. yamlContent, err := ioutil.ReadFile("fixtures.yaml")
  2. yamlOutput := yaml.MapSlice{}
  3. err := yaml.Unmarshal(yamlContent, &yamlOutput)
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. for _, tableData := range yamlOutput {
  8. //Table Name
  9. fmt.Println(tableData.Key)
  10. //Table Data
  11. fmt.Println(tableData.Value)
  12. // Error here
  13. for _, row := range tableData.Value {
  14. fmt.Println(row)
  15. }
  16. }

The value of tableData.Value looks something like this:

  1. [[{id 1} {field1 some value} {field2 some value} {field3 some value} {field4 some value} {field5 some value}]]

The problem is I cannot range through tableData.Value. Whenever I do, I get the error:

  1. cannot range over tableData.Value (type interface {})

But whenever I use reflect.TypeOf(tableData.Value), I get []interface {}

What should I do to be able to loop through each row and then through each key value pair? I'm pretty new in Go so I'm not really sure what to do next.

答案1

得分: 1

如果你有一个静态类型为interface{}的值,其动态类型为[]interface{},那么你需要对其进行type-assert,以便能够在切片上进行迭代。

  1. if v, ok := tableData.Value.([]interface{}); ok {
  2. for _, row := range v {
  3. fmt.Println(row)
  4. }
  5. }

https://play.golang.org/p/D16JKPglcaW

英文:

If you have a value that has static type interface{} whose dynamic type is []interface{} then you have to type-assert it to the dynamic type to be able to range over the slice.

  1. if v, ok := tableData.Value.([]interface{}); ok {
  2. for _, row := range v {
  3. fmt.Println(row)
  4. }
  5. }

https://play.golang.org/p/D16JKPglcaW

huangapple
  • 本文由 发表于 2021年10月18日 11:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/69610539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定