Skip to content

Instantly share code, notes, and snippets.

@tdshipley
Last active August 31, 2015 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdshipley/8e4f626551e9be06e4dc to your computer and use it in GitHub Desktop.
Save tdshipley/8e4f626551e9be06e4dc to your computer and use it in GitHub Desktop.
An example of marshling and unmarshling YAML in GO
package main
import (
"gopkg.in/yaml.v2"
)
var data = `
- name: 'Thomas'
- age: 25
`
type Person struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
}
func main() {
person := Person{}
// Read the YAML data into a struct instance called person
err := yaml.Unmarshal([]byte(data), &person)
// Covert the person struct variable into a json variable called personJSON
personJSON, err := yaml.Marshal(&person)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment