码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Protocol Buffer 使用


    这里填写标题

    • 1. Protocol Buffer 使用
      • 1.1. Prerequisites
      • 1.2. protoc 工具使用
      • 1.3. protoc-gen-go
      • 1.4. 参考

    1. Protocol Buffer 使用

    1.1. Prerequisites

    1. Go, any one of the three latest major releases of Go.

    For installation instructions, see Go’s Getting Started guide.

    1. Protocol buffer compiler, protoc, version 3.

    For installation instructions, see Protocol Buffer Compiler Installation.

    1. Go plugins for the protocol compiler:

    Install the protocol compiler plugins for Go using the following commands:

    $ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
    $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
    
    • 1
    • 2

    Update your PATH so that the protoc compiler can find the plugins:

    $ export PATH="$PATH:$(go env GOPATH)/bin"
    
    • 1

    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
    
    • 1
    • 2
    • 3

    最简单的编译命令:

    protoc --go_out=.  *.proto
    
    • 1

    --{xxx}_out 指令是必须的, 因为要输出具体的编程语言代码。

    这个输出文件的路径是执行命令的路径, 如果我们不在 .proto 文件目录下执行命令, 则输出的代码便不是相同位置了。为了解决这个问题, 我们可以使用:

    --go_opt=paths=source_relative
    
    • 1

    这样在别的地方执行命令, 生成的代码会跟 .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
    
    • 1

    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
    
    • 1

    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
    
    • 1

    See the README and documentation for protocol buffers to learn more:

    https://developers.google.com/protocol-buffers/

    1.4. 参考

    1. Protocol Buffer Compiler Installation
    2. Quick start
  • 相关阅读:
    Leetcode191. 位1的个数
    Android 7.1.2 更换原生输入法
    微前端——qiankun(乾坤)实例
    基于ASP.NET MVC + Bootstrap的仓库管理系统
    Arcgis打开影像分析窗口没反应
    Python实现贝叶斯岭回归模型(BayesianRidge算法)并使用K折交叉验证进行模型评估项目实战
    java新特性流 stream01
    OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源
    分布式文件系统--MinIO
    常用的cmd指令
  • 原文地址:https://blog.csdn.net/wan212000/article/details/126097917
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号