Announcing go-msgpack, a rich Go library for encoding/decoding to msgpack binary format, with support for net/rpc communication.
It provides features similar to encoding packages in the standard library (ie json, xml, gob, etc).
Supports:
- Standard Marshal/Unmarshal interface.
- Standard field renaming via tags
- Encoding from any value (struct, slice, map, primitives, pointers, interface{}, etc)
- Decoding into a pointer to any non-nil value (struct, slice, map, int, float32, bool, string, etc)
- Decoding into a nil interface{} (big)
- Handles time.Time transparently (using RFC3339 format).
Usage
// v can be interface{}, int32, bool, map[string]bool, etc
dec = msgpack.NewDecoder(r)
err = dec.Decode(&v)
enc = msgpack.NewEncoder(w)
err = enc.Encode(v)
//methods below are convenience methods over functions above.
data, err = msgpack.Marshal(v)
err = msgpack.Unmarshal(data, &v)
//RPC Communication
conn, err = net.Dial("tcp", "localhost:5555")
rpcCodec := msgpack.NewRPCCodec(conn)
client := rpc.NewClientWithCodec(rpcCodec)
...
Why?
I was initially looking at different binary serialization formats for an application I'm developing. I looked at Thrift, Protocol Buffers, BSON, Msgpack, etc.
I finally decided on msgpack:
- compact
- supports basic types (just like JSON. Numbers, Bool, Null, Bytes/String, Map, List) from which other data structures else can be assembled
- raw bytes support (which can represent binary data or strings)
- no schema needed (just like JSON)
- cross-language support
- has pretty good mindshare
Unfortunately, the Go library on the
msgpack.org site is old, does not build, and is not "smart" like the encoding/json package. Specifically, it doesn't allow me decode into a typed object (e.g. struct, bool, etc).
I wrote go-msgpack to give the same conveniences we've gotten used to (spoiled by using the encoding/json package), while being really performant.
Summary of my testing:
- Decodes significantly faster (120% faster) and encodes only slightly slower (20% slower) than encoding/json
- Uses less disk space (40% less)
- May not require compression (compression only gave a 10% reduction in size).
Since compression/decompression time may be significant, this may be an important win.
Hope folks use it and enjoy using it. I know I will. Please feel free to send me feedback.