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:\n%v\n\n", 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:
I would def question if you truly have dynamic data?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With