msgpack: Index | Examples | Files | Directories

package msgpack

import ""



Package Files

decode.go decode_map.go decode_number.go decode_query.go decode_slice.go decode_string.go decode_value.go encode.go encode_map.go encode_number.go encode_slice.go encode_value.go ext.go intern.go msgpack.go time.go types.go unsafe.go

func Marshal Uses

func Marshal(v interface{}) ([]byte, error)

Marshal returns the MessagePack encoding of v.


type Item struct {
    Foo string

b, err := msgpack.Marshal(&Item{Foo: "bar"})
if err != nil {

var item Item
err = msgpack.Unmarshal(b, &item)
if err != nil {




type Item struct {
    _msgpack struct{} `msgpack:",asArray"`
    Foo      string
    Bar      string

var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
err := enc.Encode(&Item{Foo: "foo", Bar: "bar"})
if err != nil {

dec := msgpack.NewDecoder(&buf)
v, err := dec.DecodeInterface()
if err != nil {


[foo bar]


in := map[string]interface{}{"foo": 1, "hello": "world"}
b, err := msgpack.Marshal(in)
if err != nil {

var out map[string]interface{}
err = msgpack.Unmarshal(b, &out)
if err != nil {

fmt.Println("foo =", out["foo"])
fmt.Println("hello =", out["hello"])


foo = 1
hello = world


type Item struct {
    Foo string
    Bar string

item := &Item{
    Foo: "hello",
b, err := msgpack.Marshal(item)
if err != nil {
fmt.Printf("item: %q\n", b)

type ItemOmitEmpty struct {
    _msgpack struct{} `msgpack:",omitempty"`
    Foo      string
    Bar      string

itemOmitEmpty := &ItemOmitEmpty{
    Foo: "hello",
b, err = msgpack.Marshal(itemOmitEmpty)
if err != nil {
fmt.Printf("item2: %q\n", b)


item: "\x82\xa3Foo\xa5hello\xa3Bar\xa0"
item2: "\x81\xa3Foo\xa5hello"

func Register Uses

func Register(value interface{}, enc encoderFunc, dec decoderFunc)

Register registers encoder and decoder functions for a value. This is low level API and in most cases you should prefer implementing Marshaler/CustomEncoder and Unmarshaler/CustomDecoder interfaces.

func RegisterExt Uses

func RegisterExt(id int8, value interface{})

RegisterExt records a type, identified by a value for that type, under the provided id. That id will identify the concrete type of a value sent or received as an interface variable. Only types that will be transferred as implementations of interface values need to be registered. Expecting to be used only during initialization, it panics if the mapping between types and ids is not a bijection.


package main

import (


func init() {
    msgpack.RegisterExt(1, (*EventTime)(nil))

type EventTime struct {

var _ msgpack.Marshaler = (*EventTime)(nil)
var _ msgpack.Unmarshaler = (*EventTime)(nil)

func (tm *EventTime) MarshalMsgpack() ([]byte, error) {
    b := make([]byte, 8)
    binary.BigEndian.PutUint32(b, uint32(tm.Unix()))
    binary.BigEndian.PutUint32(b[4:], uint32(tm.Nanosecond()))
    return b, nil

func (tm *EventTime) UnmarshalMsgpack(b []byte) error {
    if len(b) != 8 {
        return fmt.Errorf("invalid data length: got %d, wanted 8", len(b))
    sec := binary.BigEndian.Uint32(b)
    usec := binary.BigEndian.Uint32(b[4:])
    tm.Time = time.Unix(int64(sec), int64(usec))
    return nil

func main() {
    b, err := msgpack.Marshal(&EventTime{time.Unix(123456789, 123)})
    if err != nil {

    var v interface{}
    err = msgpack.Unmarshal(b, &v)
    if err != nil {

    tm := new(EventTime)
    err = msgpack.Unmarshal(b, tm)
    if err != nil {


func Unmarshal Uses

func Unmarshal(data []byte, v interface{}) error

Unmarshal decodes the MessagePack-encoded data and stores the result in the value pointed to by v.

type CustomDecoder Uses

type CustomDecoder interface {
    DecodeMsgpack(*Decoder) error

type CustomEncoder Uses

type CustomEncoder interface {
    EncodeMsgpack(*Encoder) error


package main

import (


type customStruct struct {
    S   string
    N   int

var _ msgpack.CustomEncoder = (*customStruct)(nil)
var _ msgpack.CustomDecoder = (*customStruct)(nil)

func (s *customStruct) EncodeMsgpack(enc *msgpack.Encoder) error {
    return enc.EncodeMulti(s.S, s.N)

func (s *customStruct) DecodeMsgpack(dec *msgpack.Decoder) error {
    return dec.DecodeMulti(&s.S, &s.N)

func main() {
    b, err := msgpack.Marshal(&customStruct{S: "hello", N: 42})
    if err != nil {

    var v customStruct
    err = msgpack.Unmarshal(b, &v)
    if err != nil {
    fmt.Printf("%#v", v)

type Decoder Uses

type Decoder struct {
    // contains filtered or unexported fields

A Decoder reads and decodes MessagePack values from an input stream.

func NewDecoder Uses

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

The decoder introduces its own buffering and may read data from r beyond the MessagePack values requested. Buffering can be disabled by passing a reader that implements io.ByteScanner interface.

func (*Decoder) Buffered Uses

func (d *Decoder) Buffered() io.Reader

Buffered returns a reader of the data remaining in the Decoder's buffer. The reader is valid until the next call to Decode.

func (*Decoder) Decode Uses

func (d *Decoder) Decode(v interface{}) error


func (*Decoder) DecodeArrayLen Uses

func (d *Decoder) DecodeArrayLen() (int, error)

DecodeArrayLen decodes array length. Length is -1 when array is nil.

func (*Decoder) DecodeBool Uses

func (d *Decoder) DecodeBool() (bool, error)

func (*Decoder) DecodeBytes Uses

func (d *Decoder) DecodeBytes() ([]byte, error)

func (*Decoder) DecodeBytesLen Uses

func (d *Decoder) DecodeBytesLen() (int, error)

func (*Decoder) DecodeDuration Uses

func (d *Decoder) DecodeDuration() (time.Duration, error)

func (*Decoder) DecodeExtHeader Uses

func (d *Decoder) DecodeExtHeader() (typeID int8, length int, err error)

func (*Decoder) DecodeFloat32 Uses

func (d *Decoder) DecodeFloat32() (float32, error)

func (*Decoder) DecodeFloat64 Uses

func (d *Decoder) DecodeFloat64() (float64, error)

DecodeFloat64 decodes msgpack float32/64 into Go float64.

func (*Decoder) DecodeInt Uses

func (d *Decoder) DecodeInt() (int, error)

func (*Decoder) DecodeInt16 Uses

func (d *Decoder) DecodeInt16() (int16, error)

func (*Decoder) DecodeInt32 Uses

func (d *Decoder) DecodeInt32() (int32, error)

func (*Decoder) DecodeInt64 Uses

func (d *Decoder) DecodeInt64() (int64, error)

DecodeInt64 decodes msgpack int8/16/32/64 and uint8/16/32/64 into Go int64.

func (*Decoder) DecodeInt8 Uses

func (d *Decoder) DecodeInt8() (int8, error)

func (*Decoder) DecodeInterface Uses

func (d *Decoder) DecodeInterface() (interface{}, error)

DecodeInterface decodes value into interface. It returns following types:

- nil,
- bool,
- int8, int16, int32, int64,
- uint8, uint16, uint32, uint64,
- float32 and float64,
- string,
- []byte,
- slices of any of the above,
- maps of any of the above.

DecodeInterface should be used only when you don't know the type of value you are decoding. For example, if you are decoding number it is better to use DecodeInt64 for negative numbers and DecodeUint64 for positive numbers.

func (*Decoder) DecodeInterfaceLoose Uses

func (d *Decoder) DecodeInterfaceLoose() (interface{}, error)

DecodeInterfaceLoose is like DecodeInterface except that:

- int8, int16, and int32 are converted to int64,
- uint8, uint16, and uint32 are converted to uint64,
- float32 is converted to float64.

func (*Decoder) DecodeMap Uses

func (d *Decoder) DecodeMap() (interface{}, error)

func (*Decoder) DecodeMapLen Uses

func (d *Decoder) DecodeMapLen() (int, error)

DecodeMapLen decodes map length. Length is -1 when map is nil.

func (*Decoder) DecodeMulti Uses

func (d *Decoder) DecodeMulti(v ...interface{}) error

func (*Decoder) DecodeNil Uses

func (d *Decoder) DecodeNil() error

func (*Decoder) DecodeSlice Uses

func (d *Decoder) DecodeSlice() ([]interface{}, error)

func (*Decoder) DecodeString Uses

func (d *Decoder) DecodeString() (string, error)

func (*Decoder) DecodeTime Uses

func (d *Decoder) DecodeTime() (time.Time, error)

func (*Decoder) DecodeUint Uses

func (d *Decoder) DecodeUint() (uint, error)

func (*Decoder) DecodeUint16 Uses

func (d *Decoder) DecodeUint16() (uint16, error)

func (*Decoder) DecodeUint32 Uses

func (d *Decoder) DecodeUint32() (uint32, error)

func (*Decoder) DecodeUint64 Uses

func (d *Decoder) DecodeUint64() (uint64, error)

DecodeUint64 decodes msgpack int8/16/32/64 and uint8/16/32/64 into Go uint64.

func (*Decoder) DecodeUint8 Uses

func (d *Decoder) DecodeUint8() (uint8, error)

func (*Decoder) DecodeValue Uses

func (d *Decoder) DecodeValue(v reflect.Value) error

func (*Decoder) DisallowUnknownFields Uses

func (d *Decoder) DisallowUnknownFields()

DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

func (*Decoder) PeekCode Uses

func (d *Decoder) PeekCode() (codes.Code, error)

PeekCode returns the next MessagePack code without advancing the reader. Subpackage msgpack/codes contains list of available codes.

func (*Decoder) Query Uses

func (d *Decoder) Query(query string) ([]interface{}, error)

Query extracts data specified by the query from the msgpack stream skipping any other data. Query consists of map keys and array indexes separated with dot, e.g. key1.0.key2.


b, err := msgpack.Marshal([]map[string]interface{}{
    {"id": 1, "attrs": map[string]interface{}{"phone": 12345}},
    {"id": 2, "attrs": map[string]interface{}{"phone": 54321}},
if err != nil {

dec := msgpack.NewDecoder(bytes.NewBuffer(b))
values, err := dec.Query("*")
if err != nil {
fmt.Println("phones are", values)

values, err = dec.Query("")
if err != nil {
fmt.Println("2nd phone is", values[0])


phones are [12345 54321]
2nd phone is 54321

func (*Decoder) Reset Uses

func (d *Decoder) Reset(r io.Reader)

Reset discards any buffered data, resets all state, and switches the buffered reader to read from r.

func (*Decoder) SetDecodeMapFunc Uses

func (d *Decoder) SetDecodeMapFunc(fn func(*Decoder) (interface{}, error))


buf := new(bytes.Buffer)

enc := msgpack.NewEncoder(buf)
in := map[string]string{"hello": "world"}
err := enc.Encode(in)
if err != nil {

dec := msgpack.NewDecoder(buf)

// Causes decoder to produce map[string]string instead of map[string]interface{}.
dec.SetDecodeMapFunc(func(d *msgpack.Decoder) (interface{}, error) {
    n, err := d.DecodeMapLen()
    if err != nil {
        return nil, err

    m := make(map[string]string, n)
    for i := 0; i < n; i++ {
        mk, err := d.DecodeString()
        if err != nil {
            return nil, err

        mv, err := d.DecodeString()
        if err != nil {
            return nil, err

        m[mk] = mv
    return m, nil

out, err := dec.DecodeInterface()
if err != nil {
fmt.Printf("%#v", out)



func (*Decoder) Skip Uses

func (d *Decoder) Skip() error

Skip skips next value.

func (*Decoder) UseDecodeInterfaceLoose Uses

func (d *Decoder) UseDecodeInterfaceLoose(on bool) *Decoder

UseDecodeInterfaceLoose causes decoder to use DecodeInterfaceLoose to decode msgpack value into Go interface{}.

func (*Decoder) UseJSONTag Uses

func (d *Decoder) UseJSONTag(on bool) *Decoder

UseJSONTag causes the Decoder to use json struct tag as fallback option if there is no msgpack tag.

type Encoder Uses

type Encoder struct {
    // contains filtered or unexported fields

func NewEncoder Uses

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode Uses

func (e *Encoder) Encode(v interface{}) error

func (*Encoder) EncodeArrayLen Uses

func (e *Encoder) EncodeArrayLen(l int) error

func (*Encoder) EncodeBool Uses

func (e *Encoder) EncodeBool(value bool) error

func (*Encoder) EncodeBytes Uses

func (e *Encoder) EncodeBytes(v []byte) error

func (*Encoder) EncodeBytesLen Uses

func (e *Encoder) EncodeBytesLen(l int) error

func (*Encoder) EncodeDuration Uses

func (e *Encoder) EncodeDuration(d time.Duration) error

func (*Encoder) EncodeExtHeader Uses

func (e *Encoder) EncodeExtHeader(typeID int8, length int) error

func (*Encoder) EncodeFloat32 Uses

func (e *Encoder) EncodeFloat32(n float32) error

func (*Encoder) EncodeFloat64 Uses

func (e *Encoder) EncodeFloat64(n float64) error

func (*Encoder) EncodeInt Uses

func (e *Encoder) EncodeInt(n int64) error

EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes. Type of the number is lost during encoding.

func (*Encoder) EncodeInt16 Uses

func (e *Encoder) EncodeInt16(n int16) error

EncodeInt16 encodes an int16 in 3 bytes preserving type of the number.

func (*Encoder) EncodeInt32 Uses

func (e *Encoder) EncodeInt32(n int32) error

EncodeInt32 encodes an int32 in 5 bytes preserving type of the number.

func (*Encoder) EncodeInt64 Uses

func (e *Encoder) EncodeInt64(n int64) error

EncodeInt64 encodes an int64 in 9 bytes preserving type of the number.

func (*Encoder) EncodeInt8 Uses

func (e *Encoder) EncodeInt8(n int8) error

EncodeInt8 encodes an int8 in 2 bytes preserving type of the number.

func (*Encoder) EncodeMapLen Uses

func (e *Encoder) EncodeMapLen(l int) error

func (*Encoder) EncodeMulti Uses

func (e *Encoder) EncodeMulti(v ...interface{}) error

func (*Encoder) EncodeNil Uses

func (e *Encoder) EncodeNil() error

func (*Encoder) EncodeString Uses

func (e *Encoder) EncodeString(v string) error

func (*Encoder) EncodeTime Uses

func (e *Encoder) EncodeTime(tm time.Time) error

func (*Encoder) EncodeUint Uses

func (e *Encoder) EncodeUint(n uint64) error

EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes. Type of the number is lost during encoding.

func (*Encoder) EncodeUint16 Uses

func (e *Encoder) EncodeUint16(n uint16) error

EncodeUint16 encodes an uint16 in 3 bytes preserving type of the number.

func (*Encoder) EncodeUint32 Uses

func (e *Encoder) EncodeUint32(n uint32) error

EncodeUint32 encodes an uint16 in 5 bytes preserving type of the number.

func (*Encoder) EncodeUint64 Uses

func (e *Encoder) EncodeUint64(n uint64) error

EncodeUint64 encodes an uint16 in 9 bytes preserving type of the number.

func (*Encoder) EncodeUint8 Uses

func (e *Encoder) EncodeUint8(n uint8) error

EncodeUint8 encodes an uint8 in 2 bytes preserving type of the number.

func (*Encoder) EncodeValue Uses

func (e *Encoder) EncodeValue(v reflect.Value) error

func (*Encoder) Reset Uses

func (e *Encoder) Reset(w io.Writer)

func (*Encoder) SortMapKeys Uses

func (e *Encoder) SortMapKeys(on bool) *Encoder

SortMapKeys causes the Encoder to encode map keys in increasing order. Supported map types are:

- map[string]string
- map[string]interface{}

func (*Encoder) StructAsArray Uses

func (e *Encoder) StructAsArray(on bool) *Encoder

StructAsArray causes the Encoder to encode Go structs as msgpack arrays.

func (*Encoder) UseCompactEncoding Uses

func (e *Encoder) UseCompactEncoding(on bool) *Encoder

UseCompactEncoding causes the Encoder to chose the most compact encoding. For example, it allows to encode small Go int64 as msgpack int8 saving 7 bytes.

func (*Encoder) UseCompactFloats Uses

func (e *Encoder) UseCompactFloats(on bool)

UseCompactFloats causes the Encoder to chose a compact integer encoding for floats that can be represented as integers.

func (*Encoder) UseJSONTag Uses

func (e *Encoder) UseJSONTag(on bool) *Encoder

UseJSONTag causes the Encoder to use json struct tag as fallback option if there is no msgpack tag.

type Marshaler Uses

type Marshaler interface {
    MarshalMsgpack() ([]byte, error)

type Unmarshaler Uses

type Unmarshaler interface {
    UnmarshalMsgpack([]byte) error



Package msgpack imports 18 packages (graph) and is imported by 341 packages. Updated 2020-03-19. Refresh now. Tools for package owners.