Skip to content

Instantly share code, notes, and snippets.

@aren55555
Created July 20, 2017 21:55
Show Gist options
  • Save aren55555/c4c0a8151146c1014beeac560de7fe5b to your computer and use it in GitHub Desktop.
Save aren55555/c4c0a8151146c1014beeac560de7fe5b to your computer and use it in GitHub Desktop.
Embedded Marshal/Unmarshal
package main
import (
"encoding/json"
"fmt"
)
type Foo struct {
Bar
Name string
}
type Bar struct {
Name int
}
func main() {
f := &Foo{
Name: "foo",
Bar: Bar{
Name: 5,
},
}
b, err := json.Marshal(f)
if err != nil {
panic(err)
}
fmt.Println(string(b))
d := map[string]interface{}{
"Name": 5,
}
b, _ = json.Marshal(d)
f = &Foo{}
err = json.Unmarshal(b, f)
if err != nil {
panic(err)
}
fmt.Println(f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment