教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 go int64转string_Go语言微服务架构实战:第七节 Protobuf协议语法及原理

go int64转string_Go语言微服务架构实战:第七节 Protobuf协议语法及原理

发布时间:2022-01-01   编辑:jiaochengji.com
教程集为您提供go int64转string,Go语言微服务架构实战:第七节 Protobuf协议语法及原理等资源,欢迎您收藏本站,我们将为您提供最新的go int64转string,Go语言微服务架构实战:第七节 Protobuf协议语法及原理资源

Go语言中有对应的实现Protobuf协议的库,Github地址:https://github.com/golang/protobuf

<h3>环境准备</h3>

使用Go语言的Protobuf库之前,需要相应的环境准备:

<ul><li>1、安装protobuf编译器。 可以在如下地址:https://github.com/protocolbuffers/protobuf/releases选择适合自己系统的Proto编译器程序进行下载并解压,如图:</li></ul>

<ul><li>2、配置环境变量 protoc编译器正常运行需要进行环境变量配置,将protocke执行文件所在目录添加到当前系统的环境变量中。windows系统下可以直接在Path目录中进行添加;macOS系统下可以将protoc可执行文件拷贝至/usr/local/include目录下。具体的对应的系统的环境变量配置可以阅读解压后与bin目录同级的readme.txt的文件内容。</li></ul><h3>安装</h3>

通过如下命令安装protoc-gen-go库:

<pre><code>go get github.com/golang/protobuf/protoc-gen-go</code></pre>

安装完成以后,protoc-gen-go*可执行文件在本地环境GOPATH/bin目录下,如下图所示:

<h3>Protobuf 协议语法</h3> <ul><li>Protobuf 协议的格式 Protobuf协议规定:使用该协议进行数据序列化和反序列化操作时,首先定义传输数据的格式,并命名为以".proto"为扩展名的消息定义文件。</li><li>message 定义一个消息 先来看一个非常简单的例子。假设想定义一个“订单”的消息格式,每一个“订单"都含有一个订单号ID、订单金额Num、订单时间TimeStamp字段。可以采用如下的方式来定义消息类型的.proto文件:</li></ul>
<pre><code>message Order{ required string order_id = 1; required int64 num = 2; optional int32 timestamp = 3; } Order消息格式有3个字段,在消息中承载的数据分别对应每一个字段。其中每个字段都有一个名字和一种类型。</code></pre>
<ul><li> <ul><li>指定字段类型:在proto协议中,字段的类型包括字符串(string)、整形(int32、int64...)、枚举(enum)等数据类型</li><li>分配标识符:在消息字段中,每个字段都有唯一的一个标识符。最小的标识号可以从1开始,最大到536870911。不可以使用其中的[19000-19999]的标识号, Protobuf协议实现中对这些进行了预留。如果非要在.proto文件中使用这些预留标识号,编译时就会报警。</li><li>指定字段规则:字段的修饰符包含三种类型,分别是: <ul><li>required:一个格式良好的消息一定要含有1个这种字段。表示该值是必须要设置的;</li><li>optional:消息格式中该字段可以有0个或1个值(不超过1个)。</li><li>repeated:在一个格式良好的消息中,这种字段可以重复任意多次(包括0次)。重复的值的顺序会被保留。表示该值可以重复,相当于Go中的slice。</li></ul></li></ul></li></ul>

【注意:】使用required弊多于利;在实际开发中更应该使用optional和repeated而不是required。

<ul><li> <ul><li>添加更多消息类型 在同一个.proto文件中,可以定义多个消息类型。多个消息类型分开定义即可。</li></ul></li></ul><h3>使用 Protobuf 的步骤</h3> <ul><li>1、创建扩展名为.proto的文件,并编写代码。比如创建person.proto文件,内容如下:</li></ul>
<pre><code>syntax = "proto2"; package example; ​ message Person { required string Name = 1; required int32 Age = 2; required string From = 3; }</code></pre>
<ul><li>2、编译.proto文件,生成Go语言文件。执行如下命令:</li></ul>
<pre><code>protoc --go_out = . test.proto</code></pre>

执行 protoc --go_out=. test.proto 生成对应的 person.pb.go 文件。并构建对应的example目录,存放生成的person.pb.go文件。

<ul><li>3、在程序中使用Protobuf 在程序中有如下代码:</li></ul>
<pre><code>package main import ( "fmt" "ProtocDemo/example" "github.com/golang/protobuf/proto" "os" ) func main() { fmt.Println("Hello World. n") ​ msg_test := &example.Person{ Name: proto.String("Davie"), Age: proto.Int(18), From: proto.String("China"), } ​ //序列化 msgDataEncoding, err := proto.Marshal(msg_test) if err != nil { panic(err.Error()) return } ​ msgEntity := example.Person{} err = proto.Unmarshal(msgDataEncoding, &msgEntity) if err != nil { fmt.Println(err.Error()) os.Exit(1) return } ​ fmt.Printf("姓名:%snn", msgEntity.GetName()) fmt.Printf("年龄:%dnn", msgEntity.GetAge()) fmt.Printf("国籍:%snn", msgEntity.GetFrom()) }</code></pre>
<ul><li>3、执行程序</li></ul>

到此这篇关于“go int64转string_Go语言微服务架构实战:第七节 Protobuf协议语法及原理”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
go int64转string_Go语言微服务架构实战:第七节 Protobuf协议语法及原理
golang微服务框架对比_Golang 中的微服务 - 第一部分
Golang 微服务教程(一)
golang微服务框架对比_Golang 微服务教程(一)
Go 语言进阶教程
PHP 微服务集群搭建 - Hyperf
想系统学习GO语言(Golang
golang微服务框架对比_斗鱼开源首秀——基于 Go 的微服务框架 Jupiter
支持多语言的微服务框架Tars-Go
今日头条 Go 建千亿级微服务的实践

[关闭]
~ ~