go -生成pb文件

在使用 protoc 工具生成 go 代码的时候报出的如下错误:
在syntax = “proto3”; //版本号版本中字段默认为syntax = “proto3”; //版本号
在这里插入图片描述

protoc ./test.proto --go_out=./

 WARNING: Missing 'go_package' option in "message.proto",
please specify it with the full Go package path as
a future release of protoc-gen-go will require this be specified.

是因为在 proto3 的语法中缺少了 option go_package。
解决:
在syntax下面添加option信息

option go_package = "student;student";
student 表示生成的go文件的存放地址,会自动生成目录的。
stu表示生成的go文件所属的包名

比如:

option go_package = "student;stu";

test.proto


syntax = "proto3"; //版本号
option go_package = "student;stu";
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;
}
enum ClassName{
    
       //枚举
    class1=0;  //标号 必须从 0开始
    class2=1;
    class3=2;
}

proto_test为我工程的目录
在这里插入图片描述
最后生成的结构如上
test.pb.go

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// 	protoc-gen-go v1.25.0-devel
// 	protoc        (unknown)
// source: test.proto

package stu

import (
	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)
)

type ClassName int32

const (
	ClassName_class1 ClassName = 0 //标号 必须从 0开始
	ClassName_class2 ClassName = 1
	ClassName_class3 ClassName = 2
)

// Enum value maps for ClassName.
var (
	ClassName_name = map[int32]string{
    
    
		0: "class1",
		1: "class2",
		2: "class3",
	}
	ClassName_value = map[string]int32{
    
    
		"class1": 0,
		"class2": 1,
		"class3": 2,
	}
)

func (x ClassName) Enum() *ClassName {
    
    
	p := new(ClassName)
	*p = x
	return p
}

func (x ClassName) String() string {
    
    
	return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}

func (ClassName) Descriptor() protoreflect.EnumDescriptor {
    
    
	return file_test_proto_enumTypes[0].Descriptor()
}

func (ClassName) Type() protoreflect.EnumType {
    
    
	return &file_test_proto_enumTypes[0]
}

func (x ClassName) Number() protoreflect.EnumNumber {
    
    
	return protoreflect.EnumNumber(x)
}

// Deprecated: Use ClassName.Descriptor instead.
func (ClassName) EnumDescriptor() ([]byte, []int) {
    
    
	return file_test_proto_rawDescGZIP(), []int{
    
    0}
}

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:标号,唯一 即可(相当于数据库中的Id,不一定要从1 ,2的顺序依次排列。)
	Age     int32     `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`  //必须指定整型的范围,如int32,int64
	Address string    `protobuf:"bytes,3,opt,name=address,proto3" json:"address,omitempty"`
	Cn      ClassName `protobuf:"varint,4,opt,name=cn,proto3,enum=ClassName" json:"cn,omitempty"`
}

func (x *Student) Reset() {
    
    
	*x = Student{
    
    }
	if protoimpl.UnsafeEnabled {
    
    
		mi := &file_test_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_test_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_test_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
}

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

func (x *Student) GetCn() ClassName {
    
    
	if x != nil {
    
    
		return x.Cn
	}
	return ClassName_class1
}

type Students struct {
    
    
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Person []*Student `protobuf:"bytes,1,rep,name=person,proto3" json:"person,omitempty"` // repeated 修饰,相当于Go中切片
	School string     `protobuf:"bytes,2,opt,name=school,proto3" json:"school,omitempty"`
}

func (x *Students) Reset() {
    
    
	*x = Students{
    
    }
	if protoimpl.UnsafeEnabled {
    
    
		mi := &file_test_proto_msgTypes[1]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

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

func (*Students) ProtoMessage() {
    
    }

func (x *Students) ProtoReflect() protoreflect.Message {
    
    
	mi := &file_test_proto_msgTypes[1]
	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 Students.ProtoReflect.Descriptor instead.
func (*Students) Descriptor() ([]byte, []int) {
    
    
	return file_test_proto_rawDescGZIP(), []int{
    
    1}
}

func (x *Students) GetPerson() []*Student {
    
    
	if x != nil {
    
    
		return x.Person
	}
	return nil
}

func (x *Students) GetSchool() string {
    
    
	if x != nil {
    
    
		return x.School
	}
	return ""
}

var File_test_proto protoreflect.FileDescriptor

var file_test_proto_rawDesc = []byte{
    
    
	0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x65, 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, 0x12, 0x18, 0x0a,
	0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07,
	0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x1a, 0x0a, 0x02, 0x63, 0x6e, 0x18, 0x04, 0x20,
	0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x4e, 0x61, 0x6d, 0x65, 0x52,
	0x02, 0x63, 0x6e, 0x22, 0x44, 0x0a, 0x08, 0x53, 0x74, 0x75, 0x64, 0x65, 0x6e, 0x74, 0x73, 0x12,
	0x20, 0x0a, 0x06, 0x70, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32,
	0x08, 0x2e, 0x53, 0x74, 0x75, 0x64, 0x65, 0x6e, 0x74, 0x52, 0x06, 0x70, 0x65, 0x72, 0x73, 0x6f,
	0x6e, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x6f, 0x6f, 0x6c, 0x18, 0x02, 0x20, 0x01, 0x28,
	0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x6f, 0x6f, 0x6c, 0x2a, 0x2f, 0x0a, 0x09, 0x43, 0x6c, 0x61,
	0x73, 0x73, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x31,
	0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x32, 0x10, 0x01, 0x12, 0x0a,
	0x0a, 0x06, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x33, 0x10, 0x02, 0x42, 0x0d, 0x5a, 0x0b, 0x73, 0x74,
	0x75, 0x64, 0x65, 0x6e, 0x74, 0x3b, 0x73, 0x74, 0x75, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f,
	0x33,
}

var (
	file_test_proto_rawDescOnce sync.Once
	file_test_proto_rawDescData = file_test_proto_rawDesc
)

func file_test_proto_rawDescGZIP() []byte {
    
    
	file_test_proto_rawDescOnce.Do(func() {
    
    
		file_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_test_proto_rawDescData)
	})
	return file_test_proto_rawDescData
}

var file_test_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_test_proto_goTypes = []interface{
    
    }{
    
    
	(ClassName)(0),   // 0: ClassName
	(*Student)(nil),  // 1: Student
	(*Students)(nil), // 2: Students
}
var file_test_proto_depIdxs = []int32{
    
    
	0, // 0: Student.cn:type_name -> ClassName
	1, // 1: Students.person:type_name -> Student
	2, // [2:2] is the sub-list for method output_type
	2, // [2:2] is the sub-list for method input_type
	2, // [2:2] is the sub-list for extension type_name
	2, // [2:2] is the sub-list for extension extendee
	0, // [0:2] is the sub-list for field type_name
}

func init() {
    
     file_test_proto_init() }
func file_test_proto_init() {
    
    
	if File_test_proto != nil {
    
    
		return
	}
	if !protoimpl.UnsafeEnabled {
    
    
		file_test_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
			}
		}
		file_test_proto_msgTypes[1].Exporter = func(v interface{
    
    }, i int) interface{
    
    } {
    
    
			switch v := v.(*Students); 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_test_proto_rawDesc,
			NumEnums:      1,
			NumMessages:   2,
			NumExtensions: 0,
			NumServices:   0,
		},
		GoTypes:           file_test_proto_goTypes,
		DependencyIndexes: file_test_proto_depIdxs,
		EnumInfos:         file_test_proto_enumTypes,
		MessageInfos:      file_test_proto_msgTypes,
	}.Build()
	File_test_proto = out.File
	file_test_proto_rawDesc = nil
	file_test_proto_goTypes = nil
	file_test_proto_depIdxs = nil
}

2.遇到的问题

ubuntu@VM-0-17-ubuntu:~/go/testpb$ go build
testpb.go:7:2: unknown import path "/home/ubuntu/go/testpb/testpb.go": internal error: module loader did not resolve import
testpb.go:6:2: no required module provides package github.com/golang/protobuf/proto; to add it:
        go get github.com/golang/protobuf/proto
ubuntu@VM-0-17-ubuntu:~/go/testpb$ go get github.com/golang/protobuf/proto
go get: module github.com/golang/protobuf/proto: Get "https://proxy.golang.org/github.com/golang/protobuf/proto/@v/list": dial tcp 172.217.160.81:443: i/o timeout
ubuntu@VM-0-17-ubuntu:~/go/testpb$ 
ubuntu@VM-0-17-ubuntu:~/go/testpb$ go env -w GOPROXY=https://goproxy.cn
ubuntu@VM-0-17-ubuntu:~/go/testpb$ go get github.com/golang/protobuf/proto
go: downloading github.com/golang/protobuf v1.5.2
go: downloading google.golang.org/protobuf v1.26.0
go get: added github.com/golang/protobuf v1.5.2

猜你喜欢

转载自blog.csdn.net/alpha_love/article/details/115423244