var data = []byte{0x82, 0xa3, 0x6b, 0x65, 0x79, 0xcc, 0x14, 0xa3, 0x6e, 0x61, 0x6d, 0x65, 0xa5, 0x4a, 0x6f, 0x68, 0x6e, 0x6e, 0xa4, 0x61, 0x67, 0x65, 0x20} r := bytes.NewReader(data) size, err := msgp.NewReader(r).ReadMapHeader() if err != nil { // handle error } fmt.Printf("Map size: %d\n", size) // Output: Map size: 2
var data = []byte{0xa2, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72} r := bytes.NewReader(data) _, err := msgp.NewReader(r).ReadMapHeader() if err != nil { // handle error } key, err := msgp.NewReader(r).ReadString() if err != nil { // handle error } val, err := msgp.NewReader(r).ReadString() if err != nil { // handle error } fmt.Printf("%s: %s\n", key, val) // Output: foo: barThis example reads the header of a map from a byte slice and then reads the first key and its corresponding value from the stream. The MessagePack data is constructed as follows: `0xa2` indicates that the map has two key-value pairs, `0xa3` indicates the length of the first key (3 bytes), `0x66, 0x6f, 0x6f` is the first key "foo", `0xa3` indicates the length of the first value (3 bytes), `0x62, 0x61, 0x72` is the first value "bar".