Go Yaml 解释示例

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

Go Yaml Interpretation Example

问题

这是一个Go Yaml解析的示例,可以解析一个简单的YAML数据结构:

foo: 1
bar:
  - one
  - two

现在我想解析一个包含相同数据结构的数组,应该如何正确操作?

以下是我目前的代码,但它不能按预期工作。请帮忙看看。

package main

import (
	"fmt"
	"log"

	"gopkg.in/yaml.v2"
)

type Config struct {
	Foo string
	Bar []string
}

type Configs struct {
	Cfgs []Config
}

var data = `
- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3
`

func main() {

  var config Configs
  
	/*
	   filename := os.Args[1]
	   source, err := ioutil.ReadFile(filename)
	   if err != nil {
	       panic(err)
	   }
	*/

	source := []byte(data)

	err := yaml.Unmarshal(source, &config)
	if err != nil {
		log.Fatalf("error: %v", err)
	}

	//fmt.Printf("Value: %#v\n", config.Bar[0])
	
	fmt.Printf("--- config:\n%v\n\n", config)
}

更新:

为了使其工作,Jfly指出,只需将我的var config Configs替换为var config []Config,就可以解决问题。现在我认为,如果将我的数据定义更改为以下内容,我的上述代码将起作用。

foobars:
 - foo: 1
   bar:
    - one
    - two
    - three

 - foo: 2
   bar:
    - one1
    - two2
    - three3

不好意思,它还是不起作用。请帮忙看看。

英文:

This Go Yaml Interpretation Example,
https://gist.github.com/suntong001/74c85d15b19ef4b14b0e, can unmarshal a simple YAML like this:

foo: 1
bar:
  - one
  - two

Now I want to interpretation an array of the exact same data structure, what's the correct way to do?

Below is my code so far, and it is not working as expected. Please help.

package main

import (
	"fmt"
	"log"

	"gopkg.in/yaml.v2"
)

type Config struct {
	Foo string
	Bar []string
}

type Configs struct {
	Cfgs []Config
}

var data = `
- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3
`

func main() {

  var config Configs
  
	/*
	   filename := os.Args[1]
	   source, err := ioutil.ReadFile(filename)
	   if err != nil {
	       panic(err)
	   }
	*/

	source := []byte(data)

	err := yaml.Unmarshal(source, &config)
	if err != nil {
		log.Fatalf("error: %v", err)
	}

	//fmt.Printf("Value: %#v\n", config.Bar[0])
	
	fmt.Printf("--- config:\n%v\n\n", config)
}

UPDATE:

To make it work, Jfly pointed out, simply replace my var config Configs with var config []Config, and it does the trick. Now I think if change my data definition to the following, my above code would work.

foobars:
 - foo: 1
   bar:
    - one
    - two
    - three

 - foo: 2
   bar:
    - one1
    - two2
    - three3

Nops, it doesn't. Please help.

答案1

得分: 1

示例yaml文件的内容是对象序列,可以按照以下方式处理:

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v2"
)

type Config struct {
    Foo string
    Bar []string
}

var data = `
- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3
`

func main() {

    var config []Config

    /*
       filename := os.Args[1]
       source, err := ioutil.ReadFile(filename)
       if err != nil {
           panic(err)
       }
    */

    source := []byte(data)

    err := yaml.Unmarshal(source, &config)
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    //fmt.Printf("Value: %#v\n", config.Bar[0])

    fmt.Printf("--- config:\n%v\n\n", config)
}

至于你更新的问题,你的代码几乎可以工作,只需给yaml解析器一个提示,像这样:

type Configs struct {
    Cfgs []Config `yaml:"foobars"`
}
英文:

The contents of the example yaml file are sequence of objects, so do it like this:

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v2"
)

type Config struct {
    Foo string
    Bar []string
}

var data = `
- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3
`

func main() {

    var config []Config

    /*
       filename := os.Args[1]
       source, err := ioutil.ReadFile(filename)
       if err != nil {
           panic(err)
       }
    */

    source := []byte(data)

    err := yaml.Unmarshal(source, &config)
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    //fmt.Printf("Value: %#v\n", config.Bar[0])

    fmt.Printf("--- config:\n%v\n\n", config)
}

As to your updated question, your code almost works, just give a hint to the yaml parser like this:

type Configs struct {
    Cfgs []Config `foobars`
}

huangapple
  • 本文由 发表于 2015年6月15日 12:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/30837357.html
匿名

发表评论

匿名网友

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

确定