dongtou2016 2015-06-15 04:27
浏览 44
已采纳

Go Yaml解释示例

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
", config.Bar[0])

    fmt.Printf("--- config:
%v

", 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条回答 默认 最新

  • doucheng9304 2015-06-15 10:55
    关注

    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
    ", config.Bar[0])
    
        fmt.Printf("--- config:
    %v
    
    ", 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`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了