当前位置:网站首页>Protocol Buffer usage
Protocol Buffer usage
2022-08-01 20:34:00 【Cloud full of notes】
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. 参考
边栏推荐
- Remove 360's detection and modification of the default browser
- 98.嵌入式控制器EC实战 EC开发板开发完成
- "Torch" tensor multiplication: matmul, einsum
- Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"
- 我的驾照考试笔记(1)
- vant实现Select效果--单选和多选
- Get started quickly with MongoDB
- 【luogu P1912】诗人小G(二分栈)(决策单调性优化DP)
- Which websites are commonly used for patent searches?
- 】 【 nn. The Parameter () to generate and why do you want to initialize
猜你喜欢

Remove 360's detection and modification of the default browser
![[Multi-task model] Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized (RecSys'20)](/img/7d/f8f150ad13f4cacc143491fcd8420b.png)
[Multi-task model] Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized (RecSys'20)

To promote energy conservation institute 】 【 the opinions of the agricultural water price reform

宝塔搭建PESCMS-Ticket开源客服工单系统源码实测

Little data on how to learn?Jida latest small learning data review, 26 PDF page covers the 269 - page document small data learning theory, method and application are expounded

4.1 配置Mysql与注册登录模块

我的驾照考试笔记(3)

Where should I prepare for the PMP exam in September?

泰德制药董事长郑翔玲荣膺“2022卓越影响力企业家奖” 泰德制药荣获“企业社会责任典范奖”

【Untitled】
随机推荐
excel高级绘图技巧100讲(二十二)-如何对不规则数据进行分列
Custom command to get focus
[Personal work] Wireless network image transmission module
MongoDB快速上手
OSG笔记:设置DO_NOT_COMPUTE_NEAR_FAR,手动计算远近平面
因斯布鲁克大学团队量子计算硬件突破了二进制
iptables的使用简单测试
【无标题】
The configuration manual for the secondary development of the XE training system of the missing moment document system
idea插件generateAllSetMethod一键生成set/get方法以及bean对象转换
What is the difference between a utility model patent and an invention patent?Understand in seconds!
AQS原理和介绍
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
Qt设置应用程序开机自启 解决设置失败原因
Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"
环境变量,进程地址空间
使用微信公众号给指定微信用户发送信息
专利检索常用的网站有哪些?
洛谷 P2440 木材加工
Fork/Join线程池