Protobuf生成的go源码分析

创建一个protobuf消息文件

syntax = "proto3";

option go_package = "./;blog";

package blog;

message User{
    int32 uid =1;
    string uname = 2;
    int32 age = 3;
}

message Article{
    int32 aid = 1;
    string title = 2;
    int32 views = 3;
};

编译生成go源码

protoc  --go_out=./blog ./blog/*.proto

源码分析

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// 	protoc-gen-go v1.27.1
// 	protoc        v3.19.3
// source: blog/blog.proto

package blog

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 User struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Uid   int32  `protobuf:"varint,1,opt,name=uid,proto3" json:"uid,omitempty"`
	Uname string `protobuf:"bytes,2,opt,name=uname,proto3" json:"uname,omitempty"`
	Age   int32  `protobuf:"varint,3,opt,name=age,proto3" json:"age,omitempty"`
}

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

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

func (*User) ProtoMessage() {}

func (x *User) ProtoReflect() protoreflect.Message {
	mi := &file_blog_blog_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 User.ProtoReflect.Descriptor instead.
func (*User) Descriptor() ([]byte, []int) {
	return file_blog_blog_proto_rawDescGZIP(), []int{0}
}

func (x *User) GetUid() int32 {
	if x != nil {
		return x.Uid
	}
	return 0
}

func (x *User) GetUname() string {
	if x != nil {
		return x.Uname
	}
	return ""
}

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

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

	Aid   int32  `protobuf:"varint,1,opt,name=aid,proto3" json:"aid,omitempty"`
	Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
	Views int32  `protobuf:"varint,3,opt,name=views,proto3" json:"views,omitempty"`
}

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

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

func (*Article) ProtoMessage() {}

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

func (x *Article) GetAid() int32 {
	if x != nil {
		return x.Aid
	}
	return 0
}

func (x *Article) GetTitle() string {
	if x != nil {
		return x.Title
	}
	return ""
}

func (x *Article) GetViews() int32 {
	if x != nil {
		return x.Views
	}
	return 0
}

var File_blog_blog_proto protoreflect.FileDescriptor

var file_blog_blog_proto_rawDesc = []byte{
	0x0a, 0x0f, 0x62, 0x6c, 0x6f, 0x67, 0x2f, 0x62, 0x6c, 0x6f, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74,
	0x6f, 0x12, 0x04, 0x62, 0x6c, 0x6f, 0x67, 0x22, 0x40, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12,
	0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x75, 0x69,
	0x64, 0x12, 0x14, 0x0a, 0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
	0x52, 0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x03,
	0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x61, 0x67, 0x65, 0x22, 0x47, 0x0a, 0x07, 0x41, 0x72, 0x74,
	0x69, 0x63, 0x6c, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
	0x05, 0x52, 0x03, 0x61, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18,
	0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x12, 0x14, 0x0a, 0x05,
	0x76, 0x69, 0x65, 0x77, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x76, 0x69, 0x65,
	0x77, 0x73, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x3b, 0x62, 0x6c, 0x6f, 0x67, 0x62, 0x06, 0x70,
	0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var (
	file_blog_blog_proto_rawDescOnce sync.Once
	file_blog_blog_proto_rawDescData = file_blog_blog_proto_rawDesc
)

func file_blog_blog_proto_rawDescGZIP() []byte {
	file_blog_blog_proto_rawDescOnce.Do(func() {
		file_blog_blog_proto_rawDescData = protoimpl.X.CompressGZIP(file_blog_blog_proto_rawDescData)
	})
	return file_blog_blog_proto_rawDescData
}

var file_blog_blog_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_blog_blog_proto_goTypes = []interface{}{
	(*User)(nil),    // 0: blog.User
	(*Article)(nil), // 1: blog.Article
}
var file_blog_blog_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_blog_blog_proto_init() }
func file_blog_blog_proto_init() {
	if File_blog_blog_proto != nil {
		return
	}
	if !protoimpl.UnsafeEnabled {
		file_blog_blog_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*User); i {
			case 0:
				return &v.state
			case 1:
				return &v.sizeCache
			case 2:
				return &v.unknownFields
			default:
				return nil
			}
		}
		file_blog_blog_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*Article); 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_blog_blog_proto_rawDesc,
			NumEnums:      0,
			NumMessages:   2,
			NumExtensions: 0,
			NumServices:   0,
		},
		GoTypes:           file_blog_blog_proto_goTypes,
		DependencyIndexes: file_blog_blog_proto_depIdxs,
		MessageInfos:      file_blog_blog_proto_msgTypes,
	}.Build()
	File_blog_blog_proto = out.File
	file_blog_blog_proto_rawDesc = nil
	file_blog_blog_proto_goTypes = nil
	file_blog_blog_proto_depIdxs = nil
}

main.go测试

package main

import (
	"fmt"
	"pro01/blog"
)

func main() {
	article := &blog.Article{
		Aid:   1,
		Title: "protobuf for golang",
		Views: 100,
	}

	aid := article.GetAid()
	fmt.Printf("aid: %v\n", aid)
	title := article.GetTitle()
	fmt.Printf("title: %v\n", title)

	fmt.Printf("---------------")

	article.Aid = 2
	article.Title = "protobuf change "
	article.Views = 200

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ProtobufProtocol Buffers)是一种轻量级的数据序列化格式,用于结构化数据的存储和交换。它由Google开发,并且支持多种编程语言,包括C++。 要生成C++文件,首先需要定义一个.proto文件,该文件描述了数据的结构和字段。然后使用protobuf编译器将.proto文件编译成C++代码。 以下是protobuf生成C++文件的步骤: 1. 定义.proto文件:创建一个文本文件,使用protobuf的语法来定义消息类型和字段。例如,创建一个名为example.proto的文件,其中包含以下内容: ``` syntax = "proto3"; message Person { string name = 1; int32 age = 2; repeated string hobbies = 3; } ``` 2. 使用protobuf编译器生成C++代码:打开终端或命令提示符,导航到.proto文件所在的目录,并运行以下命令: ``` protoc -I=. --cpp_out=. example.proto ``` 这将在当前目录下生成一个名为example.pb.h和example.pb.cc的C++文件。 3. 在C++项目中使用生成的代码:将生成的C++文件(example.pb.h和example.pb.cc)添加到你的C++项目中,并确保你的项目中包含了protobuf库。 4. 使用生成的代码进行序列化和反序列化:在你的C++代码中,可以使用生成的代码来创建、序列化和反序列化消息对象。例如: ```cpp #include "example.pb.h" int main() { // 创建一个Person对象 Person person; person.set_name("Alice"); person.set_age(25); person.add_hobbies("reading"); person.add_hobbies("hiking"); // 将Person对象序列化为字节流 std::string serialized_data; person.SerializeToString(&serialized_data); // 反序列化字节流为Person对象 Person deserialized_person; deserialized_person.ParseFromString(serialized_data); // 访问Person对象的字段 std::cout << "Name: " << deserialized_person.name() << std::endl; std::cout << "Age: " << deserialized_person.age() << std::endl; for (const auto& hobby : deserialized_person.hobbies()) { std::cout << "Hobby: " << hobby << std::endl; } return 0; } ``` 这是一个简单的示例,演示了如何使用protobuf生成的C++代码来创建、序列化和反序列化消息对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值