vmihailenco/msgpack
GitHub: vmihailenco/msgpack
一个为 Go 语言提供高效 MessagePack 二进制序列化与反序列化能力的开源库。
Stars: 2662 | Forks: 255
# Golang 的 MessagePack 编码
[](https://travis-ci.org/vmihailenco/msgpack)
[](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5)
[](https://msgpack.uptrace.dev/)
[](https://discord.gg/rWtp5Aj)
## 资源
- [文档](https://msgpack.uptrace.dev)
- [聊天](https://discord.gg/rWtp5Aj)
- [参考](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5)
- [示例](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#pkg-examples)
## 特性
- 基本类型、数组、map、结构体、time.Time 和 interface{}。
- Appengine \*datastore.Key 和 datastore.Cursor。
- 用于自定义编码的 [CustomEncoder]/[CustomDecoder] 接口。
- 用于编码的 [扩展](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#example-RegisterExt)
类型信息。
- 通过 `msgpack:"my_field_name"` 重命名字段,以及通过 `msgpack:"alias:another_name"` 设置别名。
- 通过 `msgpack:",omitempty"` tag 省略单个空字段,或
[结构体中的所有空字段](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#example-Marshal-OmitEmpty)。
- [Map 键排序](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#Encoder.SetSortMapKeys)。
- 将所有
[结构体编码/解码为数组](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#Encoder.UseArrayEncodedStructs)
或
[单个结构体](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#example-Marshal-AsArray)。
- [Encoder.SetCustomStructTag] 和 [Decoder.SetCustomStructTag] 可以将 msgpack 变成
任何 tag 的直接替代品。
- 简单但非常快速高效的
[查询](https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#example-Decoder.Query)。
## 安装
msgpack 支持最近的 2 个 Go 版本,并且需要支持
[Go modules](https://github.com/golang/go/wiki/Modules)。因此,请确保初始化一个 Go module:
```
go mod init github.com/my/repo
```
然后安装 msgpack/v5(注意导入中的 _v5_;遗漏它是一个常见的错误):
```
go get github.com/vmihailenco/msgpack/v5
```
## 快速开始
```
import "github.com/vmihailenco/msgpack/v5"
func ExampleMarshal() {
type Item struct {
Foo string
}
b, err := msgpack.Marshal(&Item{Foo: "bar"})
if err != nil {
panic(err)
}
var item Item
err = msgpack.Unmarshal(b, &item)
if err != nil {
panic(err)
}
fmt.Println(item.Foo)
// Output: bar
}
```
## 另请参阅
- 适用于 PostgreSQL、MySQL、MSSQL 和 SQLite 的 [Golang ORM](https://github.com/uptrace/bun)
- [Golang PostgreSQL](https://bun.uptrace.dev/postgres/)
- [Golang HTTP router](https://github.com/uptrace/bunrouter)
- [Golang ClickHouse ORM](https://github.com/uptrace/go-clickhouse)
## 贡献者
感谢所有已经做出贡献的人们!
标签:EVTX分析, Go, MessagePack, Ruby工具, 序列化, 开发库, 数据编码, 日志审计