GoLang – 迭代数据以解组多个 YAML 结构

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

GoLang - Iterate over data to unmarshal multiple YAML structures

问题

我对Golang还比较新手,请原谅我的新手问题。

我目前正在使用yaml.v2包(https://github.com/go-yaml/yaml)将YAML数据解组为结构体。

考虑以下示例代码:

package main

import (
  "fmt"
  "gopkg.in/yaml.v2"
  "log"
)

type Container struct {
  First  string
  Second struct {
    Nested1 string
    Nested2 string
    Nested3 string
    Nested4 int
  }
}

var data = `
  first: first value
  second:
    nested1: GET
    nested2: /bin/bash
    nested3: /usr/local/bin/customscript
    nested4: 8080

  first: second value
  second:
    nested1: POST
    nested2: /bin/ksh
    nested3: /usr/local/bin/customscript2
    nested4: 8081
`

func main() {

  container := Container{}

  err := yaml.Unmarshal([]byte(data), &container)
  if err != nil {
    log.Fatalf("error: %v", err)
  }
  fmt.Printf("---values found:\n%+v\n\n", container)

}

结果为:

---values found: {First:second value Second:{Nested1:POST Nested2:/bin/ksh Nested3:/usr/local/bin/customscript2 Nested4:8081}}

这是预期的结果,unmarshal函数找到了一个YAML数据的实例。

我想做的是编写一个简单的while/each/for循环,循环遍历data变量,并将所有出现的数据解组为单独的Container结构体。我该如何实现这个目标?

英文:

I am fairly new to Golang, please excuse my newbyness.

I am currently using yaml.v2 package (https://github.com/go-yaml/yaml) to unmarshal YAML data into structs.

Consider the following example code:

package main

import (
  "fmt"
  "gopkg.in/yaml.v2"
  "log"
)

type Container struct {
  First  string
  Second struct {
    Nested1 string
    Nested2 string
    Nested3 string
    Nested4 int
  }
}

var data = `
  first: first value
  second:
    nested1: GET
    nested2: /bin/bash
    nested3: /usr/local/bin/customscript
    nested4: 8080

  first: second value
  second:
    nested1: POST
    nested2: /bin/ksh
    nested3: /usr/local/bin/customscript2
    nested4: 8081
`

func main() {

  container := Container{}

  err := yaml.Unmarshal([]byte(data), &container)
  if err != nil {
    log.Fatalf("error: %v", err)
  }
  fmt.Printf("---values found:\n%+v\n\n", container)

}

The result:

---values found: {First:second value Second:{Nested1:POST Nested2:/bin/ksh Nested3:/usr/local/bin/customscript2 Nested4:8081}}

This is as expected, the unmarshal function finds one occurrence of the YAML data.

What I would like to do is write a simple while/each/for loop that loops through the data variable and marshals all the occurrences into seperate Container structs. How could I achieve this?

答案1

得分: 3

要实现你想要的简单更改,可以将yaml中的数据作为数组项,然后将其解组为Container的切片。

var data = `
- first: first value
  second:
    nested1: GET
    nested2: /bin/bash
    nested3: /usr/local/bin/customscript
    nested4: 8080

- first: second value
  second:
    nested1: POST
    nested2: /bin/ksh
    nested3: /usr/local/bin/customscript2
    nested4: 8081
`

func main() {

    container := []Container{}

    err := yaml.Unmarshal([]byte(data), &container)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("---values found:\n%+v\n\n", container)

}

输出结果为:

---values found:
[{First:first value Second:{Nested1:GET Nested2:/bin/bash Nested3:/usr/local/bin/customscript Nested4:8080}} {First:second value Second:{Nested1:POST Nested2:/bin/ksh Nested3:/usr/local/bin/customscript2 Nested4:8081}}]
英文:

A simple change to accomplish what you want is to have the data in the yaml be items in an array, then Unmarshal into a slice of Container

var data = `
- first: first value
  second:
    nested1: GET
    nested2: /bin/bash
    nested3: /usr/local/bin/customscript
    nested4: 8080

- first: second value
  second:
    nested1: POST
    nested2: /bin/ksh
    nested3: /usr/local/bin/customscript2
    nested4: 8081
`

func main() {

	container := []Container{}

	err := yaml.Unmarshal([]byte(data), &container)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("---values found:\n%+v\n\n", container)

}

---values found:
[{First:first value Second:{Nested1:GET Nested2:/bin/bash Nested3:/usr/local/bin/customscript Nested4:8080}} {First:second value Second:{Nested1:POST Nested2:/bin/ksh Nested3:/usr/local/bin/customscript2 Nested4:8081}}]

huangapple
  • 本文由 发表于 2015年3月31日 02:29:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/29353542.html
匿名

发表评论

匿名网友

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

确定