Protobuf生成的go源码分析

本文介绍了如何使用protobuf创建User和Article消息文件,并展示如何将其编译为Go源码。通过详细解释了User和Article结构,以及如何在Go中操作这些结构,读者将理解如何在实际项目中利用protobuf进行数据交换。
摘要由CSDN通过智能技术生成

创建一个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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值