windows ubuntu liunx 安装protoc 使用protobuf

一、安装protoc

因为最近疫情原因,在家里的系统开发,使用的是windows  公司使用的ubutnu

所以从新安装了一遍开发环境

这里记下 protobuf安装过成。

过程中遇到了一些报错,都是安装protoc 使用 protobuf报的错误。

下面粘贴出来比如找不到下面这些错误信息。

go get 找不到 google.golang.org/protobuf 

是…\github.com\golang\protobuf\proto\buffer.go:文件缺少google.golang.org/protobuf/encoding/prototext包
其他报错也类似

解决办法:

首先在官方网站下载插件包 protoc-3.12.1-win64.zip

1. 安装编译器protoc

下载地址:https://github.com/google/protobuf/releases

window:

    下载: protoc-3.3.0-win32.zip

    解压,把bin目录下的protoc.exe复制到GOPATH/bin下,GOPATH/bin加入环境变量。

    当然也可放在其他目录,需加入环境变量,能让系统找到protoc.exe

linux:

    下载:protoc-3.3.0-linux-x86_64.zip 或 protoc-3.3.0-linux-x86_32.zip

    解压,把bin目录下的protoc复制到GOPATH/bin下,GOPATH/bin加入环境变量。

    如果喜欢编译安装的,也可下载源码自行安装,最后将可执行文件加入环境变量。

检验是否安装成功:protoc --version  或 protoc --help(使用说明)

2. 安装编译器插件protoc-gen-go (protoc-gen-go用于生成Go语言代码)

 进入GOPATH目录,并运行

>  go get -u github.com/golang/protobuf/protoc-gen-go

>  cd  github.com/golang/protobuf/protoc-gen-go

>  go  build

>  go  install

如果第二步骤报错那就看提示的什么错误,并解决它。有的人可能是上边的文件目录啊什么的会放错。

$GOPATH/bin中会生成protoc-gen-go.exe

这时候就是这样的:

这时候重新编译项目,发现可以正常运行了就。

二、使用protobuf

新建文件

syntax="proto3"; //版本号
package pro;  //包名
enum ClassName{   //枚举
    class1=0;  //标号 必须从 0开始
    class2=1;
    class3=2;
}
message Student{ //消息,对应于Go的结构体
  string name=1; //1:标号,唯一 即可(相当于数据库中的Id,不一定要从1 ,2的顺序依次排列。)
  int32 age=2;  //必须指定整型的范围,如int32,int64
  string address=3;
  ClassName cn=4;
}
message Students{
   repeated Student person=1;  // repeated 修饰,相当于Go中切片
   string school=2;
}

然后在文件的目录下执行命令:

protoc --go_out=.  test.proto
 

这个时候就会看到多出来了一个文件 叫做 test.pb.go

这个时候就开始写测试例子了,新建main.go

package main

import (
	"GoKnowledge/pro"
	"fmt"
	"github.com/golang/protobuf/proto"
)

func main(){
	s1:=&pro.Student{} //第一个学生信息
	s1.Name="jz01"
	s1.Age=23
	s1.Address="cq"
	s1.Cn=pro.ClassName_class2 //枚举类型赋值

	ss:=&pro.Students{}
	ss.Person=append(ss.Person,s1) //将第一个学生信息添加到Students对应的切片中

	s2:=&pro.Student{}  //第二个学生信息
	s2.Name="jz02"
	s2.Age=25
	s2.Address="cd"
	s2.Cn=pro.ClassName_class3

	ss.Person=append(ss.Person,s2)//将第二个学生信息添加到Students对应的切片中
	ss.School="cqu"
	fmt.Println("Students信息为:",ss)

	// Marshal takes a protocol buffer message
	// and encodes it into the wire format, returning the data.
	buffer, _ := proto.Marshal(ss)
	fmt.Println("序列化之后的信息为:",buffer)
	// 	Use UnmarshalMerge to preserve and append to existing data.
	data:=&pro.Students{}
	proto.Unmarshal(buffer,data)
	fmt.Println("反序列化之后的信息为:",data)

}

 运行起来打印:

API server listening at: 127.0.0.1:51937
Students信息为: person:{name:"jz01" age:23 address:"cq" cn:class2} person:{name:"jz02" age:25 address:"cd" cn:class3} school:"cqu"
序列化之后的信息为: [10 14 10 4 106 122 48 49 16 23 26 2 99 113 32 1 10 14 10 4 106 122 48 50 16 25 26 2 99 100 32 2 18 3 99 113 117]
反序列化之后的信息为: person:{name:"jz01" age:23 address:"cq" cn:class2} person:{name:"jz02" age:25 address:"cd" cn:class3} school:"cqu"

Debugger finished with exit code 0

OK,到此一个从安装到使用的例子,成功了。欢迎大家试试吧!

猜你喜欢

转载自blog.csdn.net/u010919083/article/details/106369334