Golang Protobuf

編譯安裝編譯器protoc

wget https://github.com/protocolbuffers/protobuf/releases/download/v2.6.1/protobuf-2.6.1.zip
unzip protobuf-2.6.1.zip
cd protobuf-2.6.1

./configure CC=clang CXX=clang++ CXXFLAGS='-std=c++11 -stdlib=libc++ -O3 -g' LDFLAGS='-stdlib=libc++' LIBS="-lc++ -lc++abi"

make -j 4 
sudo make install
protoc --version

安裝插件

go get -u github.com/golang/protobuf/protoc-gen-go

定義消息類型

// student.proto
syntax = "proto3";
package main;

// this is a comment
message Student {
  string name = 1;
  bool male = 2;
  repeated int32 scores = 3;
}

在當(dāng)前目錄下執(zhí)行:

protoc --go_out=. *.proto

ls
# student.pb.go  student.proto

protoc 生成的代碼:

// student.pb.go
package proto

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

// this is a comment
type Student struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Name   string  `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
    Male   bool    `protobuf:"varint,2,opt,name=male,proto3" json:"male,omitempty"`
    Scores []int32 `protobuf:"varint,3,rep,packed,name=scores,proto3" json:"scores,omitempty"`
}

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) GetMale() bool {
    if x != nil {
        return x.Male
    }
    return false
}

func (x *Student) GetScores() []int32 {
    if x != nil {
        return x.Scores
    }
    return nil
}

var File_student_proto protoreflect.FileDescriptor

var file_student_proto_rawDesc = []byte{
    0x0a, 0x0d, 0x73, 0x74, 0x75, 0x64, 0x65, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12,
    0x04, 0x6d, 0x61, 0x69, 0x6e, 0x22, 0x49, 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, 0x12, 0x0a, 0x04, 0x6d, 0x61, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01,
    0x28, 0x08, 0x52, 0x04, 0x6d, 0x61, 0x6c, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x6f, 0x72,
    0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x05, 0x52, 0x06, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x73,
    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: main.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
}


參考:

https://gist.github.com/liushooter/44bd8e2f6f277f2631841787aafe3d9d

https://geektutu.com/post/quick-go-protobuf.html

https://colobu.com/2019/10/03/protobuf-ultimate-tutorial-in-go/

https://www.cnblogs.com/sanshengshui/p/9739521.html

http://yura415.github.io/js-protobuf-encode-decode/

https://zhuanlan.zhihu.com/p/147391981

https://zhuanlan.zhihu.com/p/95134068

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末痛黎,一起剝皮案震驚了整個濱河市幢踏,隨后出現(xiàn)的幾起案子却妨,更是在濱河造成了極大的恐慌华临,老刑警劉巖铃拇,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铆隘,死亡現(xiàn)場離奇詭異又沾,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)归露,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門洲脂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人剧包,你說我怎么就攤上這事恐锦。” “怎么了疆液?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵一铅,是天一觀的道長。 經(jīng)常有香客問我堕油,道長潘飘,這世上最難降的妖魔是什么肮之? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮卜录,結(jié)果婚禮上局骤,老公的妹妹穿的比我還像新娘。我一直安慰自己暴凑,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布赘来。 她就那樣靜靜地躺著现喳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪犬辰。 梳的紋絲不亂的頭發(fā)上嗦篱,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機(jī)與錄音幌缝,去河邊找鬼灸促。 笑死,一個胖子當(dāng)著我的面吹牛涵卵,可吹牛的內(nèi)容都是我干的浴栽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼轿偎,長吁一口氣:“原來是場噩夢啊……” “哼典鸡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起坏晦,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤萝玷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后昆婿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體球碉,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年仓蛆,在試婚紗的時候發(fā)現(xiàn)自己被綠了睁冬。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡多律,死狀恐怖痴突,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情狼荞,我是刑警寧澤辽装,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站相味,受9級特大地震影響拾积,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一拓巧、第九天 我趴在偏房一處隱蔽的房頂上張望斯碌。 院中可真熱鬧,春花似錦肛度、人聲如沸傻唾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽冠骄。三九已至,卻和暖如春加袋,著一層夾襖步出監(jiān)牢的瞬間凛辣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工职烧, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留扁誓,地道東北人。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓蚀之,卻偏偏與公主長得像蝗敢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子恬总,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359