From 737a4340ae05f1dd8ca5881cf4df50ad05ff4c1e Mon Sep 17 00:00:00 2001 From: yumaojun03 <719118794@qq.com> Date: Sun, 16 Feb 2025 14:56:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85protobuf=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- skills/protobuf/README.md | 36 ++++++ skills/protobuf/hello.pb.go | 176 ++++++++++++++++++++++++++ skills/protobuf/hello.proto | 23 ++++ skills/rpc/hello_world/server/main.go | 1 + 5 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 skills/protobuf/README.md create mode 100644 skills/protobuf/hello.pb.go create mode 100644 skills/protobuf/hello.proto diff --git a/go.mod b/go.mod index 9d1325b..c654dde 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/infraboard/mcube/v2 v2.0.44 github.com/rs/zerolog v1.32.0 golang.org/x/crypto v0.23.0 + google.golang.org/protobuf v1.34.1 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v3 v3.0.1 gorm.io/driver/mysql v1.5.7 @@ -81,7 +82,6 @@ require ( google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect google.golang.org/grpc v1.63.2 // indirect - google.golang.org/protobuf v1.34.1 // indirect gorm.io/driver/postgres v1.5.9 // indirect modernc.org/libc v1.37.6 // indirect modernc.org/mathutil v1.6.0 // indirect diff --git a/skills/protobuf/README.md b/skills/protobuf/README.md new file mode 100644 index 0000000..69282b4 --- /dev/null +++ b/skills/protobuf/README.md @@ -0,0 +1,36 @@ +# Probuf 基础使用 + +[大纲](https://gitee.com/infraboard/go-course/blob/master/day15/protobuf.md) + +```json +{"name": "bob", "age": 10} +``` + +``` +bob10 +: 数据结构的长度: 开始位置0: offset: 3 +|bob|10| +1: "name" +2: "age" +``` + + +## 安装 + +1. protoc +```sh +$ protoc --version +libprotoc 3.19.1 +``` + +2. go 语言插件 + +```sh +go install google.golang.org/protobuf/cmd/protoc-gen-go@latest +``` + +3. +```sh +# 项目的根目录 作为命令执行目录 -I=. +$ protoc -I=. --go_out=. --go_opt=module="gitlab.com/go-course-project/go17" skills/protobuf/hello.proto +``` diff --git a/skills/protobuf/hello.pb.go b/skills/protobuf/hello.pb.go new file mode 100644 index 0000000..dc7c9d8 --- /dev/null +++ b/skills/protobuf/hello.pb.go @@ -0,0 +1,176 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.5 +// protoc v5.29.3 +// source: skills/protobuf/hello.proto + +package protobuf + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +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 HelloRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + MyName string `protobuf:"bytes,1,opt,name=my_name,json=myName,proto3" json:"my_name,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *HelloRequest) Reset() { + *x = HelloRequest{} + mi := &file_skills_protobuf_hello_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *HelloRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HelloRequest) ProtoMessage() {} + +func (x *HelloRequest) ProtoReflect() protoreflect.Message { + mi := &file_skills_protobuf_hello_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HelloRequest.ProtoReflect.Descriptor instead. +func (*HelloRequest) Descriptor() ([]byte, []int) { + return file_skills_protobuf_hello_proto_rawDescGZIP(), []int{0} +} + +func (x *HelloRequest) GetMyName() string { + if x != nil { + return x.MyName + } + return "" +} + +type HelloResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *HelloResponse) Reset() { + *x = HelloResponse{} + mi := &file_skills_protobuf_hello_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *HelloResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HelloResponse) ProtoMessage() {} + +func (x *HelloResponse) ProtoReflect() protoreflect.Message { + mi := &file_skills_protobuf_hello_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HelloResponse.ProtoReflect.Descriptor instead. +func (*HelloResponse) Descriptor() ([]byte, []int) { + return file_skills_protobuf_hello_proto_rawDescGZIP(), []int{1} +} + +func (x *HelloResponse) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +var File_skills_protobuf_hello_proto protoreflect.FileDescriptor + +var file_skills_protobuf_hello_proto_rawDesc = string([]byte{ + 0x0a, 0x1b, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, + 0x66, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x68, + 0x65, 0x6c, 0x6c, 0x6f, 0x22, 0x27, 0x0a, 0x0c, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x6d, 0x79, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x29, 0x0a, + 0x0d, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, + 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x42, 0x33, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x6c, + 0x61, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x2d, 0x63, 0x6f, 0x75, 0x72, 0x73, 0x65, + 0x2d, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x2f, 0x67, 0x6f, 0x31, 0x37, 0x2f, 0x73, 0x6b, + 0x69, 0x6c, 0x6c, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, +}) + +var ( + file_skills_protobuf_hello_proto_rawDescOnce sync.Once + file_skills_protobuf_hello_proto_rawDescData []byte +) + +func file_skills_protobuf_hello_proto_rawDescGZIP() []byte { + file_skills_protobuf_hello_proto_rawDescOnce.Do(func() { + file_skills_protobuf_hello_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_skills_protobuf_hello_proto_rawDesc), len(file_skills_protobuf_hello_proto_rawDesc))) + }) + return file_skills_protobuf_hello_proto_rawDescData +} + +var file_skills_protobuf_hello_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_skills_protobuf_hello_proto_goTypes = []any{ + (*HelloRequest)(nil), // 0: hello.HelloRequest + (*HelloResponse)(nil), // 1: hello.HelloResponse +} +var file_skills_protobuf_hello_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_skills_protobuf_hello_proto_init() } +func file_skills_protobuf_hello_proto_init() { + if File_skills_protobuf_hello_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_skills_protobuf_hello_proto_rawDesc), len(file_skills_protobuf_hello_proto_rawDesc)), + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_skills_protobuf_hello_proto_goTypes, + DependencyIndexes: file_skills_protobuf_hello_proto_depIdxs, + MessageInfos: file_skills_protobuf_hello_proto_msgTypes, + }.Build() + File_skills_protobuf_hello_proto = out.File + file_skills_protobuf_hello_proto_goTypes = nil + file_skills_protobuf_hello_proto_depIdxs = nil +} diff --git a/skills/protobuf/hello.proto b/skills/protobuf/hello.proto new file mode 100644 index 0000000..1792e61 --- /dev/null +++ b/skills/protobuf/hello.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; + +package hello; +option go_package="gitlab.com/go-course-project/go17/skills/protobuf"; + +// type HelloRequest struct { +// MyName string `json:"my_name"` +// } + +// type HelloResponse struct { +// Message string `json:"message"` +// } + +message HelloRequest { + string my_name = 1; +} + +message HelloResponse { + string message = 1; +} + + + diff --git a/skills/rpc/hello_world/server/main.go b/skills/rpc/hello_world/server/main.go index f3950fb..d60ebc3 100644 --- a/skills/rpc/hello_world/server/main.go +++ b/skills/rpc/hello_world/server/main.go @@ -53,5 +53,6 @@ func main() { rpc.ServeRequest(jsonrpc.NewServerCodec(conn)) }) + // protobuf.HelloRequest.MyName http.ListenAndServe(":1234", nil) }