title: Parse YAML with structs
body: <p>I’ve created the following YAML file to provide some config which users need to provide:</p>
  
  <pre><code>Environments:
   sys1:
      models:
      - app-type: app1
        service-type: “fds"
  
      - app-type: app2
        service-type: “era”
   sys2:
      models:
      - app-type: app1
        service-type: “fds"
  
      - app-type: app2
        service-type: “era"
  </code></pre>
  
  <p><a href="https://codebeautify.org/yaml-validator/cbb349ec" rel="noreferrer">https://codebeautify.org/yaml-validator/cbb349ec</a></p>
  
  <p>Here I have:</p>
  
  <ol>
  <li>one environment (root)</li>
  <li>the environment contain 1..n <code>sys</code></li>
  <li>Each <code>sys</code> contain 1..n models instance with key app-type</li>
  </ol>
  
  <p>Now I need to parse this YAML file so I try to build a struct type like:</p>
  
  <pre><code>type Environment struct {
      Environment [] sys
  }
  
  type sys struct{
      Models    []Properties
  }
  
  type Models struct{
      app-type     string      `yaml:"app-type"`
      service-type string      `yaml:"service-type"`
  }
  </code></pre>
  
  <p>Now I try to parse this YAML, and I get an error of index out of range.</p>
  
  <p>My questions are:</p>
  
  <pre><code>1. Do I model the YAML correctly?
  2. Do I model the struct correctly?
  </code></pre>
  
  <p>This is the code:</p>
  
  <pre><code>func main() {
      y := Environments{}
  
      err := yaml.Unmarshal([]byte(data), &amp;y)
      if err != nil {
          log.Fatalf("error: %v", err)
      }
      fmt.Printf("%+v\n", y)
  }
  </code></pre>
  
  <p>data is the <code>yaml.file</code>.</p>