Protobuf(二)[环境搭建-Golang]

1.Ubuntu1804LTS

1.Golang环境搭建

1.安装工具

sudo apt install -y wget curl tar unzip tree

2.下载golang包

wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz && ls | grep go

在这里插入图片描述

3.解压到/usr/local/

sudo tar -zxvf go1.14.2.linux-amd64.tar.gz -C /usr/local
ls /usr/local/ | grep go

在这里插入图片描述

4.创建工作目录

mkdir -p ~/workspace/go/bin ~/workspace/go/src ~/workspace/go/pkg ~/workspace/go/src && tree ~/workspace/go/

在这里插入图片描述

5.设置环境变量

tee -a 是追加

sudo tee -a /etc/profile <<-'EOF'
export GOROOT=/usr/local/go
export GOPATH=/home/wyf/workspace/go
export GOBIN=/home/wyf/workspace/go/bin
export GO111MODULE="on"
export GOPROXY=https://goproxy.io
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin:$GOBIN:$GOPROXY
EOF
source /etc/profile

在这里插入图片描述

6.测试

go version
go env

在这里插入图片描述

2.golang-protobuf

1.下载

go get -u -v github.com/golang/protobuf

在这里插入图片描述

ls ~/workspace/go/pkg

在这里插入图片描述

2.编译

1.查找protoc-gen-go

sudo find / -name "protoc-gen-go"

在这里插入图片描述

2.进入目录

cd /home/wyf/workspace/go/pkg/mod/github.com/golang/[email protected]/protoc-gen-go && pwd

1和2两步骤可以使用使用集成的命令:

查找并进入目录

cd `sudo find / -name "protoc-gen-go"` && pwd

在这里插入图片描述

3.切换root

su root

4.执行build

/usr/local/go/bin/go build

5.确定编译完成

ls -la 

在这里插入图片描述

6.拷贝到/bin

cp protoc-gen-go /bin/

在这里插入图片描述

7.退出root

exit

在这里插入图片描述

3.测试

终端输入protoc-,然后按下Tab键查看是否补全,补全即OK!

protoc-

在这里插入图片描述

2.CentOS-7

1.安装工具

sudo yum install -y wget curl tar unzip tree

2.下载golang包

wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz && ls | grep go

在这里插入图片描述

3.解压到/usr/local/

sudo tar -zxvf go1.14.2.linux-amd64.tar.gz -C /usr/local
ls /usr/local/ | grep go

在这里插入图片描述

4.创建工作目录

mkdir -p ~/workspace/go/bin ~/workspace/go/src ~/workspace/go/pkg ~/workspace/go/src && tree ~/workspace/go/

在这里插入图片描述

5.设置环境变量

tee -a 是追加

sudo tee -a /etc/profile <<-'EOF'
export GOROOT=/usr/local/go
export GOPATH=/home/wyf/workspace/go
export GOBIN=/home/wyf/workspace/go/bin
export GO111MODULE="on"
export GOPROXY=https://goproxy.io
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin:$GOBIN:$GOPROXY
EOF
source /etc/profile

在这里插入图片描述

6.测试

go version
go env

在这里插入图片描述

2.golang-protobuf

1.下载

go get -u -v github.com/golang/protobuf

在这里插入图片描述

ls ~/workspace/go/pkg

在这里插入图片描述

2.编译

1.查找protoc-gen-go

sudo find / -name "protoc-gen-go"

在这里插入图片描述

2.进入目录

cd /home/wyf/workspace/go/pkg/mod/github.com/golang/[email protected]/protoc-gen-go && pwd

1和2两步骤可以使用使用集成的命令:

查找并进入目录

cd `sudo find / -name "protoc-gen-go"` && pwd

3.切换root

su root

4.执行build

go build 

5.确定编译完成

ls -la 

在这里插入图片描述

6.拷贝到/bin

cp protoc-gen-go /bin/

7.退出root

exit

在这里插入图片描述

3.测试

终端输入protoc-,然后按下Tab键查看是否补全,补全即OK!

protoc-

在这里插入图片描述

3.测试

1.创建工程

mkdir -p $GOPATH/src/goprotobuf && cd $_ && pwd

在这里插入图片描述

2.go mod 初始化

go mod init goprotobuf

在这里插入图片描述

3.创建protobuf文件目录

mkdir -p $GOPATH/src/goprotobuf/pb && cd $_ && pwd

在这里插入图片描述

4.创建.proto文件

vim Student.proto

内容如下:

syntax = "proto3"; //指定版本信息,不指定会报错

package pb;

// message为关键字,作用为定义一种消息类型
message Student{
    string name = 1;// 序号1
    int32 age = 2; // 序号2
}

在这里插入图片描述

5.编译成go文件

1.标准格式

protoc --proto_path=IMPORT_PATH --go_out=DST_DIR path/to/file.proto

2.通用格式

cd $GOPATH/src/goprotobuf/pb/

将当前目录下【$GOPATH/src/goprotobuf/pb/】的所有的.proto文件编译成go文件,放在当前目录下【$GOPATH/src/goprotobuf/pb/】

protoc --go_out=./ *.proto

编译完成后目录:
在这里插入图片描述

编译完成后的Student.pb.go文件

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// 	protoc-gen-go v1.21.0
// 	protoc        v3.11.4
// source: Student.proto

package pb

import (
	proto "github.com/golang/protobuf/proto"
	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
	reflect "reflect"
	sync "sync"
)

const (
	// Verify that this generated code is sufficiently up-to-date.
	_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
	// Verify that runtime/protoimpl is sufficiently up-to-date.
	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)

// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4

// message为关键字,作用为定义一种消息类型
type Student struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` // 序号1
	Age  int32  `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`  // 序号2
}

func (x *Student) Reset() {
	*x = Student{}
	if protoimpl.UnsafeEnabled {
		mi := &file_Student_proto_msgTypes[0]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *Student) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*Student) ProtoMessage() {}

func (x *Student) ProtoReflect() protoreflect.Message {
	mi := &file_Student_proto_msgTypes[0]
	if protoimpl.UnsafeEnabled && x != nil {
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		if ms.LoadMessageInfo() == nil {
			ms.StoreMessageInfo(mi)
		}
		return ms
	}
	return mi.MessageOf(x)
}

// Deprecated: Use Student.ProtoReflect.Descriptor instead.
func (*Student) Descriptor() ([]byte, []int) {
	return file_Student_proto_rawDescGZIP(), []int{0}
}

func (x *Student) GetName() string {
	if x != nil {
		return x.Name
	}
	return ""
}

func (x *Student) GetAge() int32 {
	if x != nil {
		return x.Age
	}
	return 0
}

var File_Student_proto protoreflect.FileDescriptor

var file_Student_proto_rawDesc = []byte{
	0x0a, 0x0d, 0x53, 0x74, 0x75, 0x64, 0x65, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12,
	0x02, 0x70, 0x62, 0x22, 0x2f, 0x0a, 0x07, 0x53, 0x74, 0x75, 0x64, 0x65, 0x6e, 0x74, 0x12, 0x12,
	0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61,
	0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52,
	0x03, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var (
	file_Student_proto_rawDescOnce sync.Once
	file_Student_proto_rawDescData = file_Student_proto_rawDesc
)

func file_Student_proto_rawDescGZIP() []byte {
	file_Student_proto_rawDescOnce.Do(func() {
		file_Student_proto_rawDescData = protoimpl.X.CompressGZIP(file_Student_proto_rawDescData)
	})
	return file_Student_proto_rawDescData
}

var file_Student_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_Student_proto_goTypes = []interface{}{
	(*Student)(nil), // 0: pb.Student
}
var file_Student_proto_depIdxs = []int32{
	0, // [0:0] is the sub-list for method output_type
	0, // [0:0] is the sub-list for method input_type
	0, // [0:0] is the sub-list for extension type_name
	0, // [0:0] is the sub-list for extension extendee
	0, // [0:0] is the sub-list for field type_name
}

func init() { file_Student_proto_init() }
func file_Student_proto_init() {
	if File_Student_proto != nil {
		return
	}
	if !protoimpl.UnsafeEnabled {
		file_Student_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*Student); i {
			case 0:
				return &v.state
			case 1:
				return &v.sizeCache
			case 2:
				return &v.unknownFields
			default:
				return nil
			}
		}
	}
	type x struct{}
	out := protoimpl.TypeBuilder{
		File: protoimpl.DescBuilder{
			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
			RawDescriptor: file_Student_proto_rawDesc,
			NumEnums:      0,
			NumMessages:   1,
			NumExtensions: 0,
			NumServices:   0,
		},
		GoTypes:           file_Student_proto_goTypes,
		DependencyIndexes: file_Student_proto_depIdxs,
		MessageInfos:      file_Student_proto_msgTypes,
	}.Build()
	File_Student_proto = out.File
	file_Student_proto_rawDesc = nil
	file_Student_proto_goTypes = nil
	file_Student_proto_depIdxs = nil
}

生成的【.pb.go】文件不允许编辑,如需要修改某些字段或者信息,则需要修改【.proto】文件然后重新编译生成即可。

6.使用protobuf协议编程

1.在工程项目目录下创建main.go文件

cd $GOPATH/src/ && touch main.go

2.编辑main.go

vim main.go

内容如下

发布了64 篇原创文章 · 获赞 14 · 访问量 4960

猜你喜欢

转载自blog.csdn.net/weixin_42366378/article/details/105708545
今日推荐