Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created February 5, 2018 09:42
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 mashiro/7a5eb9b48287285b56a5149697f15a4a to your computer and use it in GitHub Desktop.
Save mashiro/7a5eb9b48287285b56a5149697f15a4a to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
msgpack "github.com/lestrrat/go-msgpack"
)
type User struct {
ID string `json:"id" msgpack:"id"`
}
type UserWithName struct {
User
Name string `json:"name" msgpack:"name"`
}
func jsonMarshal(v interface{}) {
fmt.Println("json")
bytes, _ := json.Marshal(v)
fmt.Println(string(bytes))
var out interface{}
json.Unmarshal(bytes, &out)
fmt.Printf("%+v\n", out)
}
func msgpackMarshal(v interface{}) {
fmt.Println("msgpack")
bytes, _ := msgpack.Marshal(v)
fmt.Println(string(bytes))
var out interface{}
msgpack.Unmarshal(bytes, &out)
fmt.Printf("%+v\n", out)
}
func main() {
u := UserWithName{}
u.ID = "123"
u.Name = "fuba"
jsonMarshal(u)
msgpackMarshal(u)
}
@mashiro
Copy link
Author

mashiro commented Feb 5, 2018

json
{"id":"123","name":"fuba"}
map[id:123 name:fuba]
msgpack
Userid123namefuba
map[User:map[id:123] name:fuba]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment