Protocol Buffers(protobuf) go语言插件下载和使用,以及import不同文件夹下proto

Protocol Buffers (protobuf)是Google的跨语言、跨平台、可扩展的结构化数据序列化机制。可以在谷歌开发者网站上找到protobuf的文档。

下载protobuf及go插件

protobuf 下载地址:

https://github.com/protocolbuffers/protobuf/releases

protobuf go语言插件,新分支下载地址:

https://github.com/protocolbuffers/protobuf-go/releases

protobuf go语言插件,旧分支仓库地址:

https://github.com/golang/protobuf

注意:github.com/golang/protobuf 1.4及之后的版本其实都是新分支了,如果需要用旧分支的版本,还是选择1.4之前的版本吧。This module (github.com/golang/protobuf) contains Go bindings for protocol buffers.It has been superseded by the google.golang.org/protobuf module, which contains an updated and simplified API, support for protobuf reflection, and many other improvements. We recommend that new code use the google.golang.org/protobuf module.

编译生成protoc-gen-go

如果是新分支,不需要编译,直接下载需要的版本就好了。

旧分支编译,这里使用1.3.5的版本。下载地址:https://github.com/golang/protobuf/tags

编译前,需要安装配置GO环境,这里就不在赘述

解压protobuf-1.3.5.zip,在protoc-gen-go文件夹下,运行 go build , 生成protoc-gen-go.exe

生成.pg.go文件

版本 protocolbuffers 3.15.8 protoc-gen-go 1.3.5

同一文件夹下,放入protoc.exe,protoc-gen-go.exe,demo.proto。

demo.proto 内容如下:

// 指定语法类型为proto2  proto3更好
syntax = "proto2"; 

// 逻辑包名 无层级
package demo; 

message Demo {   
	// required 必填
	required string name = 1;
	//optional 可选
	optional int32 age = 2;
	//repeated 数组
	repeated string email = 3;
}

执行 protoc --go_out=./ ./demo.proto

即可在当前目录下生成 demo.pb.go 文件

import 导入其他目录下proto

文件目录如下

/AAA
/AAA/person.proto
/BBB
/BBB/info.proto
/common.proto
/go-protoc.bat
/protoc.exe
/protoc-gen-go.exe

person.proto 需要导入 上层文件夹的common.proto 和 另一文件夹的info.proto

go-protoc.bat 脚本如下

rem -I 引入需要的proto --go_out 指定生成的路径 后面拼接go_package指定的路径 
rem ./AAA/person.proto 选择要生成的proto文件
protoc  -I ./  -I ./BBB  --go_out=../   ./AAA/person.proto

文件内容如下:

person.proto:

// 指定语法类型为proto2  proto3更好
syntax = "proto2"; 

// 逻辑包名 无层级
package person; 

//导入proto文件
import "common.proto";
import "info.proto";

//go的包路径 ;后 可以指定包名 通常应该为一样
option go_package = "ProtobufDemo/aaa;person";

message Person {   
	optional common.Common com = 1;
	optional info.Info info = 2;
	optional string desc = 3;
}

common.proto:

// 指定语法类型为proto2  proto3更好
syntax = "proto2"; 

// 逻辑包名 无层级
package common; 

//go的包路径 ;后 可以指定包名 通常应该为一样
option go_package = "protobufdemo;protobufdemo";

message Common {   
	// required 必填
	required string name = 1;
	//optional 可选
	optional int32 age = 2;
	//repeated 数组
	repeated string email = 3;
}

info.proto :

// 指定语法类型为proto2  proto3更好
syntax = "proto2"; 

// 逻辑包名 无层级
package info; 

//go的包路径 ;后 可以指定包名 通常应该为一样
option go_package = "protobufdemo/bbb;bbb";

message Info {   
	optional string Info = 1;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
安装protobuf 1. 下载protobuf编译器:https://github.com/protocolbuffers/protobuf/releases 2. 安装protobuf编译器: ``` $ tar -xzvf protobuf-3.15.6.tar.gz $ cd protobuf-3.15.6/ $ ./configure $ make $ sudo make install $ sudo ldconfig ``` 3. 安装protobuf的Go语言支持: ``` $ go get github.com/golang/protobuf/protoc-gen-go ``` 该命令会从GitHub上下载并编译出protoc-gen-go可执行文件,该文件是一个protobuf件,用于将proto文件生成Go语言代码。 使用protobuf 1. 编写proto文件 ```protobuf syntax = "proto3"; package example; message Person { string name = 1; int32 age = 2; repeated string hobbies = 3; } ``` 2. 使用protoc命令生成Go语言代码 ``` $ protoc --go_out=. example.proto ``` 该命令会在当前目录下生成example.pb.go文件,其中包含了protobuf定义的Person类型的Go语言结构体。 3. 在Go语言程序中使用protobuf ```go package main import ( "fmt" "example" "github.com/golang/protobuf/proto" ) func main() { p := &example.Person{ Name: "Alice", Age: 20, Hobbies: []string{"swimming", "reading"}, } data, err := proto.Marshal(p) if err != nil { fmt.Println("marshal error:", err) return } p2 := &example.Person{} err = proto.Unmarshal(data, p2) if err != nil { fmt.Println("unmarshal error:", err) return } fmt.Println("p2:", p2) } ``` 该程序使用protobuf将一个Person对象编码为二进制数据,然后解码成另一个Person对象。注意,该程序需要引入github.com/golang/protobuf/proto包,该包提供了protobuf的Go语言支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值