Created
February 5, 2018 09:42
-
-
Save mashiro/7a5eb9b48287285b56a5149697f15a4a to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
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) | |
} |
Author
mashiro
commented
Feb 5, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment