Skip to content

Instantly share code, notes, and snippets.

@mwhudson
Created March 17, 2016 22:31
Show Gist options
  • Save mwhudson/00b8a6868cb9acd5462a to your computer and use it in GitHub Desktop.
Save mwhudson/00b8a6868cb9acd5462a to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
yaml "gopkg.in/yaml.v2"
)
type bar struct {
X string
}
func (bar) MarshalYAML() (interface{}, error) {
return "MarshalYAML", nil
}
func (bar) MarshalJSON() ([]byte, error) {
return []byte(`"MarshalJSON"`), nil
}
type Foo struct {
bar
}
func main() {
f := Foo{bar: bar{"x"}}
j, e := json.Marshal(f)
if e != nil {
fmt.Println("json error", e)
} else {
fmt.Println("json", string(j))
}
s, e := yaml.Marshal(f)
if e != nil {
fmt.Println("yaml error", e)
} else {
fmt.Println("yaml", string(s))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment