protobuf学习
Protobuf编解码.
谷歌在2008年开源出来的一种数据描述语言,类似于json,xml的一种东西。
了通过附带工具生成代码并实现将结构化数据序列化的功能。但是我们更关注的是Protobuf作为借口规范的描述语言,可以作为设计安全的跨语言RPC接口的基础工具。
使用流程
.proto File protoc protocol Compiler
安装编译器
https://github.com/protocolbuffers/protobuf/releases/tag/v25.1
https://github.com/protocolbuffers/protobuf/releases/download/v25.1/protobuf-25.1.tar.gz
安装go语言插件
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
cp $GOPATH/bin/protoc-gen-go /usr/local/bin/ -av
Hello Protobuf
编译proto文件
protoc -I=./ --go_out=./ --go_opt=module="gitee.com/jasmine456/vblog" demo/protobuf/hello/hello.proto
- go中的models是什么,命名空间
protoc帮我们完成了核心的两件事
- protobuf数据接口 --protoc-gen-go--> Go语言数据结构
- 为生成好的对象struct 生成基于protobuf 序列化和反序列化的方法
序列化和反序列化
使用 "google.golang.org/protobuf/proto" 提供的api来做proto格式的序列化和反序列化