Skip to content

Instantly share code, notes, and snippets.

@huljas
Created January 24, 2016 17:35
Show Gist options
  • Save huljas/14236f8313acc4994cce to your computer and use it in GitHub Desktop.
Save huljas/14236f8313acc4994cce to your computer and use it in GitHub Desktop.
Msgpack Go example
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