码农知识堂 - 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
  • 相关阅读:
    【Node.js】
    矩阵 m * M = c
    [onnxrumtime]onnxruntime和cuda对应关系表
    阿里云域名免费配置HTTPS
    Java 算法篇-链表的经典算法:有序链表去重、合并多个有序链表
    LocalDateTime获取时间类(当前时间)
    CSS从入门到精通——动画:CSS3动画执行次数和逆向播放
    nodejs+vue健身房课程预约评分系统
    Java Day9 Stream流
    应用系统中的报表开发成本值多少?
  • 原文地址:https://blog.csdn.net/wan212000/article/details/126097917
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号