title: Go Yaml Interpretation Example
body: <p>This Go Yaml Interpretation Example, 
  <a href="https://gist.github.com/suntong001/74c85d15b19ef4b14b0e" rel="nofollow">https://gist.github.com/suntong001/74c85d15b19ef4b14b0e</a>, can unmarshal a simple YAML like this:</p>
  
  <pre><code>foo: 1
  bar:
    - one
    - two
  </code></pre>
  
  <p>Now I want to interpretation an array of the exact same data structure, what's the correct way to do?</p>
  
  <p>Below is my code so far, and it is not working as expected. Please help.</p>
  
  <pre><code>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, &amp;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)
  }
  </code></pre>
  
  <p>UPDATE: </p>
  
  <p>To make it work, Jfly pointed out, simply replace my <code>var config Configs</code> with <code>var config []Config</code>, and it does the trick. Now I think if change my data definition to the following, my above code would work. </p>
  
  <pre><code>foobars:
   - foo: 1
     bar:
      - one
      - two
      - three
  
   - foo: 2
     bar:
      - one1
      - two2
      - three3
  </code></pre>
  
  <p>Nops, it doesn't. Please help.</p>