README.md (3454B)
1 # MessagePack encoding for Golang 2 3 [![Build Status](https://travis-ci.org/vmihailenco/msgpack.svg)](https://travis-ci.org/vmihailenco/msgpack) 4 [![PkgGoDev](https://pkg.go.dev/badge/github.com/vmihailenco/msgpack/v5)](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5) 5 [![Documentation](https://img.shields.io/badge/msgpack-documentation-informational)](https://msgpack.uptrace.dev/) 6 [![Chat](https://discordapp.com/api/guilds/752070105847955518/widget.png)](https://discord.gg/rWtp5Aj) 7 8 > :heart: 9 > [**Uptrace.dev** - All-in-one tool to optimize performance and monitor errors & logs](https://uptrace.dev/?utm_source=gh-msgpack&utm_campaign=gh-msgpack-var2) 10 11 - Join [Discord](https://discord.gg/rWtp5Aj) to ask questions. 12 - [Documentation](https://msgpack.uptrace.dev) 13 - [Reference](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5) 14 - [Examples](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#pkg-examples) 15 16 Other projects you may like: 17 18 - [Bun](https://bun.uptrace.dev) - fast and simple SQL client for PostgreSQL, MySQL, and SQLite. 19 - [BunRouter](https://bunrouter.uptrace.dev/) - fast and flexible HTTP router for Go. 20 21 ## Features 22 23 - Primitives, arrays, maps, structs, time.Time and interface{}. 24 - Appengine \*datastore.Key and datastore.Cursor. 25 - [CustomEncoder]/[CustomDecoder] interfaces for custom encoding. 26 - [Extensions](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#example-RegisterExt) to encode 27 type information. 28 - Renaming fields via `msgpack:"my_field_name"` and alias via `msgpack:"alias:another_name"`. 29 - Omitting individual empty fields via `msgpack:",omitempty"` tag or all 30 [empty fields in a struct](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#example-Marshal-OmitEmpty). 31 - [Map keys sorting](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#Encoder.SetSortMapKeys). 32 - Encoding/decoding all 33 [structs as arrays](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#Encoder.UseArrayEncodedStructs) 34 or 35 [individual structs](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#example-Marshal-AsArray). 36 - [Encoder.SetCustomStructTag] with [Decoder.SetCustomStructTag] can turn msgpack into drop-in 37 replacement for any tag. 38 - Simple but very fast and efficient 39 [queries](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#example-Decoder.Query). 40 41 [customencoder]: https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#CustomEncoder 42 [customdecoder]: https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#CustomDecoder 43 [encoder.setcustomstructtag]: 44 https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#Encoder.SetCustomStructTag 45 [decoder.setcustomstructtag]: 46 https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#Decoder.SetCustomStructTag 47 48 ## Installation 49 50 msgpack supports 2 last Go versions and requires support for 51 [Go modules](https://github.com/golang/go/wiki/Modules). So make sure to initialize a Go module: 52 53 ```shell 54 go mod init github.com/my/repo 55 ``` 56 57 And then install msgpack/v5 (note _v5_ in the import; omitting it is a popular mistake): 58 59 ```shell 60 go get github.com/vmihailenco/msgpack/v5 61 ``` 62 63 ## Quickstart 64 65 ```go 66 import "github.com/vmihailenco/msgpack/v5" 67 68 func ExampleMarshal() { 69 type Item struct { 70 Foo string 71 } 72 73 b, err := msgpack.Marshal(&Item{Foo: "bar"}) 74 if err != nil { 75 panic(err) 76 } 77 78 var item Item 79 err = msgpack.Unmarshal(b, &item) 80 if err != nil { 81 panic(err) 82 } 83 fmt.Println(item.Foo) 84 // Output: bar 85 } 86 ```