Created
March 17, 2016 22:31
-
-
Save mwhudson/00b8a6868cb9acd5462a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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