当前位置:网站首页>Protocol Buffer 使用
Protocol Buffer 使用
2022-08-01 20:27:00 【云满笔记】
1. Protocol Buffer 使用
1.1. Prerequisites
- Go, any one of the three latest major releases of Go.
For installation instructions, see Go’s Getting Started guide.
- Protocol buffer compiler,
protoc
, version 3.
For installation instructions, see Protocol Buffer Compiler Installation.
- Go plugins for the protocol compiler:
Install the protocol compiler plugins for Go using the following commands:
$ go install google.golang.org/protobuf/cmd/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]
Update your PATH
so that the protoc
compiler can find the plugins:
$ export PATH="$PATH:$(go env GOPATH)/bin"
1.2. protoc 工具使用
前面我们用 protoc
来编译 .proto
文件为 go 语言, 为了支持编译为 go, 需要安装 protoc-gen-go
插件, C# 可以安装 protoc-gen-zsharp
插件。
需要注意的是, 转换 .proto
为编程语言, 不一定要安装 protoc
。
例如 C# 只需要把 .proto
文件放到项目中, 通过包管理器安装一个库, 就会自动转换为相应的代码。
回归正题, 聊一下 protoc
编译 .proto
文件的命令。
protoc
常用的参数如下:
--proto_path=. #指定proto文件的路径, 填写 . 表示就在当前目录下
--go_out=. #表示编译后的文件存放路径; 如果编译的是 csharp, 则 --csharp_out
--go_opt={
xxx.proto}={
xxx.proto的路径} # 示例: --go_opt=Mprotos/bar.proto=example.com/project/protos/foo
最简单的编译命令:
protoc --go_out=. *.proto
--{xxx}_out
指令是必须的, 因为要输出具体的编程语言代码。
这个输出文件的路径是执行命令的路径, 如果我们不在 .proto
文件目录下执行命令, 则输出的代码便不是相同位置了。为了解决这个问题, 我们可以使用:
--go_opt=paths=source_relative
这样在别的地方执行命令, 生成的代码会跟 .proto
文件放在相同的位置。
1.3. protoc-gen-go
protoc-gen-go
is a plugin for the Google protocol buffer compiler to generate Go code. Install it by building this program and making it accessible within your PATH with the name:
protoc-gen-go
The ‘go’ suffix becomes part of the argument for the protocol compiler, such that it can be invoked as:
protoc --go_out=paths=source_relative:. path/to/file.proto
This generates Go bindings for the protocol buffer defined by file.proto
. With that input, the output will be written to:
path/to/file.pb.go
See the README
and documentation for protocol buffers to learn more:
https://developers.google.com/protocol-buffers/
1.4. 参考
边栏推荐
猜你喜欢
To promote energy conservation institute 】 【 the opinions of the agricultural water price reform
【Dart】dart构造函数学习记录(含dart单例模式写法)
终于有人把AB实验讲明白了
【kali-信息收集】(1.2)SNMP枚举:Snmpwalk、Snmpcheck;SMTP枚举:smtp-user-enum
57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom
Greenplum Database Source Code Analysis - Analysis of Standby Master Operation Tools
【kali-信息收集】(1.3)探测网络范围:DMitry(域名查询工具)、Scapy(跟踪路由工具)
第58章 结构、纪录与类
Oracle排序某个字段, 如果这个varchar2类型的字段有数字也有文字 , 怎么按照数字大小排序?
我的驾照考试笔记(3)
随机推荐
乐观锁批量跟新 纯SQL
Does LabVIEW really close the COM port using VISA Close?
Debug一个ECC的ODP数据源
实用新型专利和发明专利的区别?秒懂!
4.1 配置Mysql与注册登录模块
第55章 业务逻辑之订单、支付实体定义
deploy zabbix
To promote energy conservation institute 】 【 the opinions of the agricultural water price reform
Addition, Subtraction, Multiplication of Large Integers, Multiplication and Division of Large Integers and Ordinary Integers
KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System
What is the difference between a utility model patent and an invention patent?Understand in seconds!
算法---解码方法(Kotlin)
我的驾照考试笔记(2)
启明云端分享|盘点ESP8684开发板有哪些功能
使用Huggingface在矩池云快速加载预训练模型和数据集
KDD2022 | 自监督超图Transformer推荐系统
Intranet penetration lanproxy deployment
【Untitled】
9月备考PMP,应该从哪里备考?
30-day question brushing plan (5)