快速PB
一个更快的Protobuf串行器和解串器。
注意
- 现在只支持proto3。
- 现在不支持已知类型(any, api, duration...)。
安装
go >= 1.16
go install github.com/cloudwego/fastpb/protoc-gen-fastpb@latest
go <= 1.15
go get github.com/cloudwego/fastpb/protoc-gen-fastpb@latest
使用方法
参照例子,分两步使用:
第1步:生成代码
使用命令行工具来生成代码:
protoc --go_out=. \
--fastpb_out=. \
${your_idl}.proto
or
protoc --go_opt=paths=source_relative --go_out=. \
--fastpb_opt=paths=source_relative --fastpb_out=. \
${your_idl}.proto
第2步:编解码信息
编码和解码必须使用fastpb的API,显示为演示:
package examples
import (
"github.com/cloudwego/fastpb"
)
// Marshal .
func Marshal(msg fastpb.Writer) []byte {
// TODO: buffer can be reused.
buf := make([]byte, msg.Size())
msg.FastWrite(buf)
return buf
}
// Unmarshal .
func Unmarshal(buf []byte, msg fastpb.Reader) error {
_, err := fastpb.ReadMessage(buf, int8(fastpb.SkipTypeCheck), msg)
return err
}