Created
January 24, 2016 17:35
-
-
Save huljas/14236f8313acc4994cce to your computer and use it in GitHub Desktop.
Msgpack Go example
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 msgpack | |
import ( | |
"github.com/ugorji/go/codec" | |
"log" | |
//"io" | |
"bytes" | |
"testing" | |
) | |
type ExampleMessage struct { | |
Code int | |
Body map[string]interface{} | |
} | |
func TestMsgPackEncoding(t *testing.T) { | |
var sourceMsg = ExampleMessage{Code: 11, Body: map[string]interface{}{ | |
"a": 1, | |
"b": true, | |
"c": "d", | |
}} | |
var buffer = new(bytes.Buffer) | |
//var writer io.Writer = buffer | |
var handle codec.Handle = new(codec.MsgpackHandle) | |
var enc *codec.Encoder = codec.NewEncoder(buffer, handle) | |
var err error = enc.Encode(sourceMsg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
encodedBytes := buffer.Bytes() | |
var dec *codec.Decoder = codec.NewDecoderBytes(encodedBytes, handle) | |
var resMsg ExampleMessage | |
dec.Decode(&resMsg) | |
log.Print("Decoded %s", resMsg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment