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

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

Cannot loop through yaml.MapItem.Value in go

问题

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

fixtures.yaml

table1:
  - id: 1
    field1: "some value"
    field2: "some value"

table2:
  - id: 1
    field1: "some value"
    field2: "some value"

...更多数据...

main.go

yamlContent, err := ioutil.ReadFile("fixtures.yaml")
yamlOutput := yaml.MapSlice{}

err := yaml.Unmarshal(yamlContent, &yamlOutput)
if err != nil {
  log.Fatal(err)
}

for _, tableData := range yamlOutput {
  // 表格名称
  fmt.Println(tableData.Key)

  // 表格数据
  fmt.Println(tableData.Value)

  // 这里出错
  for _, row := range tableData.Value {
    fmt.Println(row)
  }
}

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

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

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

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

table1:
  -id: 1
   field1: "some value"
   field2: "some value"

table2:
  -id: 1
   field1: "some value"
   field2: "some value"

...some more data...

main.go

yamlContent, err := ioutil.ReadFile("fixtures.yaml")
yamlOutput := yaml.MapSlice{}

err := yaml.Unmarshal(yamlContent, &yamlOutput)
if err != nil {
  log.Fatal(err)
}

for _, tableData := range yamlOutput {
  //Table Name
  fmt.Println(tableData.Key)

  //Table Data
  fmt.Println(tableData.Value)

  // Error here
  for _, row := range tableData.Value {
    fmt.Println(row)
  }
}

The value of tableData.Value looks something like this:

[[{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:

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,以便能够在切片上进行迭代。

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

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.

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

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:

确定