Go Yaml 解释示例

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

Go Yaml Interpretation Example

问题

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

  1. foo: 1
  2. bar:
  3. - one
  4. - two

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. type Config struct {
  8. Foo string
  9. Bar []string
  10. }
  11. type Configs struct {
  12. Cfgs []Config
  13. }
  14. var data = `
  15. - foo: 1
  16. bar:
  17. - one
  18. - two
  19. - three
  20. - foo: 2
  21. bar:
  22. - one1
  23. - two2
  24. - three3
  25. `
  26. func main() {
  27. var config Configs
  28. /*
  29. filename := os.Args[1]
  30. source, err := ioutil.ReadFile(filename)
  31. if err != nil {
  32. panic(err)
  33. }
  34. */
  35. source := []byte(data)
  36. err := yaml.Unmarshal(source, &config)
  37. if err != nil {
  38. log.Fatalf("error: %v", err)
  39. }
  40. //fmt.Printf("Value: %#v\n", config.Bar[0])
  41. fmt.Printf("--- config:\n%v\n\n", config)
  42. }

更新:

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

  1. foobars:
  2. - foo: 1
  3. bar:
  4. - one
  5. - two
  6. - three
  7. - foo: 2
  8. bar:
  9. - one1
  10. - two2
  11. - three3

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

英文:

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

  1. foo: 1
  2. bar:
  3. - one
  4. - 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.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. type Config struct {
  8. Foo string
  9. Bar []string
  10. }
  11. type Configs struct {
  12. Cfgs []Config
  13. }
  14. var data = `
  15. - foo: 1
  16. bar:
  17. - one
  18. - two
  19. - three
  20. - foo: 2
  21. bar:
  22. - one1
  23. - two2
  24. - three3
  25. `
  26. func main() {
  27. var config Configs
  28. /*
  29. filename := os.Args[1]
  30. source, err := ioutil.ReadFile(filename)
  31. if err != nil {
  32. panic(err)
  33. }
  34. */
  35. source := []byte(data)
  36. err := yaml.Unmarshal(source, &config)
  37. if err != nil {
  38. log.Fatalf("error: %v", err)
  39. }
  40. //fmt.Printf("Value: %#v\n", config.Bar[0])
  41. fmt.Printf("--- config:\n%v\n\n", config)
  42. }

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.

  1. foobars:
  2. - foo: 1
  3. bar:
  4. - one
  5. - two
  6. - three
  7. - foo: 2
  8. bar:
  9. - one1
  10. - two2
  11. - three3

Nops, it doesn't. Please help.

答案1

得分: 1

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

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. type Config struct {
  8. Foo string
  9. Bar []string
  10. }
  11. var data = `
  12. - foo: 1
  13. bar:
  14. - one
  15. - two
  16. - three
  17. - foo: 2
  18. bar:
  19. - one1
  20. - two2
  21. - three3
  22. `
  23. func main() {
  24. var config []Config
  25. /*
  26. filename := os.Args[1]
  27. source, err := ioutil.ReadFile(filename)
  28. if err != nil {
  29. panic(err)
  30. }
  31. */
  32. source := []byte(data)
  33. err := yaml.Unmarshal(source, &config)
  34. if err != nil {
  35. log.Fatalf("error: %v", err)
  36. }
  37. //fmt.Printf("Value: %#v\n", config.Bar[0])
  38. fmt.Printf("--- config:\n%v\n\n", config)
  39. }

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

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. type Config struct {
  8. Foo string
  9. Bar []string
  10. }
  11. var data = `
  12. - foo: 1
  13. bar:
  14. - one
  15. - two
  16. - three
  17. - foo: 2
  18. bar:
  19. - one1
  20. - two2
  21. - three3
  22. `
  23. func main() {
  24. var config []Config
  25. /*
  26. filename := os.Args[1]
  27. source, err := ioutil.ReadFile(filename)
  28. if err != nil {
  29. panic(err)
  30. }
  31. */
  32. source := []byte(data)
  33. err := yaml.Unmarshal(source, &config)
  34. if err != nil {
  35. log.Fatalf("error: %v", err)
  36. }
  37. //fmt.Printf("Value: %#v\n", config.Bar[0])
  38. fmt.Printf("--- config:\n%v\n\n", config)
  39. }

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

  1. type Configs struct {
  2. Cfgs []Config `foobars`
  3. }

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:

确定