在Go中加载动态yaml结构[关闭]

I'm trying to convert some Python code to Golang, and I'm having some difficulty figuring out how to load dynamic yaml data, which I thought was pretty basic. So far, all of the approaches I have found mention creating a Struct and mapping values, but this won't be possible since the data I'll be receiving will be different every time it's read.

This isn't the real data (which will actually be returned from an API), but as an example yaml file:

[ ~]$ cat /tmp/example.yaml
Massachusetts:
  cities:
    - name: 'Boston'
      area_code: 617
    - name: 'Springfield'
    - name: 'Worcester'
Virginia:
  cities:
    - name: 'Richmond'
    - name: 'Arlington'
      landmarks:
        - 'The Pentagon'
        - 'National Airport'
        - 'Arlington National Cemetary'
  presidents:
    - 'George Washington'
    - 'Thomas Jefferson'
    - 'James Madison'
    - 'James Monroe'
    - 'William Henry Harrison'
    - 'John Tyler'
Missouri:
  rivers:
    - 'Missouri River'
    - 'Mississippi'
    - 'Arkansas River'
    - 'White River'

And reading and manipulating it in Python is simple:

#!/usr/bin/python
import yaml
with open('/tmp/example.yaml', 'r') as fh:
    data = yaml.load(fh)

print yaml.dump(data, default_flow_style=False)

As I'm new to Go, does anyone know which technique I should use / documentation I should look for that will do what this Python code does?

One of the most popular go yaml packages has this exact example in their documentation:

package main

import (
        "fmt"
        "log"

        "gopkg.in/yaml.v2"
)

var data = `
a: Easy!
b:
  c: 2
  d: [3, 4]
`

func main() {
     m := make(map[interface{}]interface{})

     err = yaml.Unmarshal([]byte(data), &m)
     if err != nil {
         log.Fatalf("error: %v", err)
     }
     fmt.Printf("--- m:
%v

", m)
}

As Flimzy points out above in his comment, now it is up to your app to dynamically handle the schema. I feel like this may qualify as "Schema on Read" and there are many tradeoffs to this approach vs an approach where you use a static definition of the data:

  • type assertions/inspection must be handled at runtime, after unmarshalling
  • Schema must be codified in logic and assertions instead of declared
  • ??

I would def question if you truly have dynamic data?

Here is a solution if you were to use go struct

package main

import (
    "gopkg.in/yaml.v2"
    "log"
    "fmt"
    "bytes"
)

type City struct {
    Name string
    AreaCode string `yaml:"area_code"`
    Landmarks []string
}

type State struct {
    Name string
    Cities []*City
    Rivers []string
    Presidents []string
}


type States map[string]*State

func (s *States) Unmarshal(data []byte) error {
    err := yaml.NewDecoder(bytes.NewReader(data)).Decode(s)
    if err != nil {
        return err
    }
    for k, v := range *s {
        v.Name= k
    }
    return nil
}

func main() {
    YAML  := `Massachusetts:
  cities:
    - name: 'Boston'
      area_code: 617
    - name: 'Springfield'
    - name: 'Worcester'
Virginia:
  cities:
    - name: 'Richmond'
    - name: 'Arlington'
      landmarks:
        - 'The Pentagon'
        - 'National Airport'
        - 'Arlington National Cemetary'
  presidents:
    - 'George Washington'
    - 'Thomas Jefferson'
    - 'James Madison'
    - 'James Monroe'
    - 'William Henry Harrison'
    - 'John Tyler'
Missouri:
  rivers:
    - 'Missouri River'
    - 'Mississippi'
    - 'Arkansas River'
    - 'White River'`


    var states States = map[string]*State{}
    err :=states.Unmarshal([]byte(YAML))
    if err != nil {
        log.Fatal("failed to decode: %v", err)
    }

    for k, v := range states {
        fmt.Printf("%v -> name: %v, presidents: %v, rivers: %v
", k, v.Name, v.Presidents, v.Rivers)
        if len(v.Cities) > 0 {
            fmt.Printf("cities:
")
            for _, city := range v.Cities {
                fmt.Printf("%v
", city)
            }
        }
    }

}