├── .gitignore ├── README.md ├── generate.bat ├── generate.sh ├── main.go └── src ├── complex ├── complex.pb.go └── complex.proto ├── enum_example ├── enum_example.pb.go └── enum_example.proto └── simple ├── simple.pb.go └── simple.proto /.gitignore: -------------------------------------------------------------------------------- 1 | simple.bin -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Protocol Buffers Example in Golang 2 | 3 | This is a companion repository for my [Protocol Buffers course](http://bit.ly/protocol-buffers-github) 4 | 5 | [![course logo](https://i.imgur.com/8fFmWAV.png)](http://bit.ly/protocol-buffers-github) 6 | 7 | # Content 8 | 9 | - Sample Code 10 | - Few .proto files 11 | - Read / Write to File 12 | - JSON example -------------------------------------------------------------------------------- /generate.bat: -------------------------------------------------------------------------------- 1 | protoc.exe -I src\ --go_out=src\ src\simple\simple.proto 2 | protoc.exe -I src\ --go_out=src\ src\enum_example\enum_example.proto 3 | protoc.exe -I src\ --go_out=src\ src\complex\complex.proto -------------------------------------------------------------------------------- /generate.sh: -------------------------------------------------------------------------------- 1 | protoc -I src/ --go_out=src/ src/simple/simple.proto 2 | protoc -I src/ --go_out=src/ src/enum_example/enum_example.proto 3 | protoc -I src/ --go_out=src/ src/complex/complex.proto -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "log" 7 | 8 | "github.com/simplesteph/protobuf-example-go/src/complex" 9 | "github.com/simplesteph/protobuf-example-go/src/enum_example" 10 | 11 | "github.com/golang/protobuf/jsonpb" 12 | "github.com/golang/protobuf/proto" 13 | "github.com/simplesteph/protobuf-example-go/src/simple" 14 | ) 15 | 16 | func main() { 17 | sm := doSimple() 18 | 19 | readAndWriteDemo(sm) 20 | jsonDemo(sm) 21 | 22 | doEnum() 23 | 24 | doComplex() 25 | } 26 | 27 | func doComplex() { 28 | cm := complexpb.ComplexMessage{ 29 | OneDummy: &complexpb.DummyMessage{ 30 | Id: 1, 31 | Name: "First message", 32 | }, 33 | MultipleDummy: []*complexpb.DummyMessage{ 34 | &complexpb.DummyMessage{ 35 | Id: 2, 36 | Name: "Second message", 37 | }, 38 | &complexpb.DummyMessage{ 39 | Id: 3, 40 | Name: "Third message", 41 | }, 42 | }, 43 | } 44 | 45 | fmt.Println(cm) 46 | } 47 | 48 | func doEnum() { 49 | em := enumpb.EnumMessage{ 50 | Id: 42, 51 | DayOfTheWeek: enumpb.DayOfTheWeek_THURSDAY, 52 | } 53 | 54 | em.DayOfTheWeek = enumpb.DayOfTheWeek_MONDAY 55 | fmt.Println(em) 56 | } 57 | 58 | func jsonDemo(sm proto.Message) { 59 | smAsString := toJSON(sm) 60 | fmt.Println(smAsString) 61 | 62 | sm2 := &simplepb.SimpleMessage{} 63 | fromJSON(smAsString, sm2) 64 | fmt.Println("Successfully created proto struct:", sm2) 65 | } 66 | 67 | func toJSON(pb proto.Message) string { 68 | marshaler := jsonpb.Marshaler{} 69 | out, err := marshaler.MarshalToString(pb) 70 | if err != nil { 71 | log.Fatalln("Can't convert to JSON", err) 72 | return "" 73 | } 74 | return out 75 | } 76 | 77 | func fromJSON(in string, pb proto.Message) { 78 | err := jsonpb.UnmarshalString(in, pb) 79 | if err != nil { 80 | log.Fatalln("Couldn't unmarshal the JSON into the pb struct", err) 81 | } 82 | } 83 | 84 | func readAndWriteDemo(sm proto.Message) { 85 | writeToFile("simple.bin", sm) 86 | sm2 := &simplepb.SimpleMessage{} 87 | readFromFile("simple.bin", sm2) 88 | fmt.Println("Read the content:", sm2) 89 | } 90 | 91 | func writeToFile(fname string, pb proto.Message) error { 92 | out, err := proto.Marshal(pb) 93 | if err != nil { 94 | log.Fatalln("Can't serialise to bytes", err) 95 | return err 96 | } 97 | 98 | if err := ioutil.WriteFile(fname, out, 0644); err != nil { 99 | log.Fatalln("Can't write to file", err) 100 | return err 101 | } 102 | 103 | fmt.Println("Data has been written!") 104 | return nil 105 | } 106 | 107 | func readFromFile(fname string, pb proto.Message) error { 108 | 109 | in, err := ioutil.ReadFile(fname) 110 | if err != nil { 111 | log.Fatalln("Something went wrong when reading the file", err) 112 | return err 113 | } 114 | 115 | err2 := proto.Unmarshal(in, pb) 116 | if err2 != nil { 117 | log.Fatalln("Couldn't put the bytes into the protocol buffers struct", err2) 118 | return err2 119 | } 120 | 121 | return nil 122 | } 123 | 124 | func doSimple() *simplepb.SimpleMessage { 125 | sm := simplepb.SimpleMessage{ 126 | Id: 12345, 127 | IsSimple: true, 128 | Name: "My Simple Message", 129 | SampleList: []int32{1, 4, 7, 8}, 130 | } 131 | fmt.Println(sm) 132 | 133 | sm.Name = "I renamed you" 134 | fmt.Println(sm) 135 | 136 | fmt.Println("The ID is:", sm.GetId()) 137 | 138 | return &sm 139 | } 140 | -------------------------------------------------------------------------------- /src/complex/complex.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: complex/complex.proto 3 | 4 | package complexpb 5 | 6 | import proto "github.com/golang/protobuf/proto" 7 | import fmt "fmt" 8 | import math "math" 9 | 10 | // Reference imports to suppress errors if they are not otherwise used. 11 | var _ = proto.Marshal 12 | var _ = fmt.Errorf 13 | var _ = math.Inf 14 | 15 | // This is a compile-time assertion to ensure that this generated file 16 | // is compatible with the proto package it is being compiled against. 17 | // A compilation error at this line likely means your copy of the 18 | // proto package needs to be updated. 19 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 20 | 21 | type ComplexMessage struct { 22 | OneDummy *DummyMessage `protobuf:"bytes,2,opt,name=one_dummy,json=oneDummy" json:"one_dummy,omitempty"` 23 | MultipleDummy []*DummyMessage `protobuf:"bytes,3,rep,name=multiple_dummy,json=multipleDummy" json:"multiple_dummy,omitempty"` 24 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 25 | XXX_unrecognized []byte `json:"-"` 26 | XXX_sizecache int32 `json:"-"` 27 | } 28 | 29 | func (m *ComplexMessage) Reset() { *m = ComplexMessage{} } 30 | func (m *ComplexMessage) String() string { return proto.CompactTextString(m) } 31 | func (*ComplexMessage) ProtoMessage() {} 32 | func (*ComplexMessage) Descriptor() ([]byte, []int) { 33 | return fileDescriptor_complex_a33d9ec46b6da591, []int{0} 34 | } 35 | func (m *ComplexMessage) XXX_Unmarshal(b []byte) error { 36 | return xxx_messageInfo_ComplexMessage.Unmarshal(m, b) 37 | } 38 | func (m *ComplexMessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 39 | return xxx_messageInfo_ComplexMessage.Marshal(b, m, deterministic) 40 | } 41 | func (dst *ComplexMessage) XXX_Merge(src proto.Message) { 42 | xxx_messageInfo_ComplexMessage.Merge(dst, src) 43 | } 44 | func (m *ComplexMessage) XXX_Size() int { 45 | return xxx_messageInfo_ComplexMessage.Size(m) 46 | } 47 | func (m *ComplexMessage) XXX_DiscardUnknown() { 48 | xxx_messageInfo_ComplexMessage.DiscardUnknown(m) 49 | } 50 | 51 | var xxx_messageInfo_ComplexMessage proto.InternalMessageInfo 52 | 53 | func (m *ComplexMessage) GetOneDummy() *DummyMessage { 54 | if m != nil { 55 | return m.OneDummy 56 | } 57 | return nil 58 | } 59 | 60 | func (m *ComplexMessage) GetMultipleDummy() []*DummyMessage { 61 | if m != nil { 62 | return m.MultipleDummy 63 | } 64 | return nil 65 | } 66 | 67 | type DummyMessage struct { 68 | Id int32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"` 69 | Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"` 70 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 71 | XXX_unrecognized []byte `json:"-"` 72 | XXX_sizecache int32 `json:"-"` 73 | } 74 | 75 | func (m *DummyMessage) Reset() { *m = DummyMessage{} } 76 | func (m *DummyMessage) String() string { return proto.CompactTextString(m) } 77 | func (*DummyMessage) ProtoMessage() {} 78 | func (*DummyMessage) Descriptor() ([]byte, []int) { 79 | return fileDescriptor_complex_a33d9ec46b6da591, []int{1} 80 | } 81 | func (m *DummyMessage) XXX_Unmarshal(b []byte) error { 82 | return xxx_messageInfo_DummyMessage.Unmarshal(m, b) 83 | } 84 | func (m *DummyMessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 85 | return xxx_messageInfo_DummyMessage.Marshal(b, m, deterministic) 86 | } 87 | func (dst *DummyMessage) XXX_Merge(src proto.Message) { 88 | xxx_messageInfo_DummyMessage.Merge(dst, src) 89 | } 90 | func (m *DummyMessage) XXX_Size() int { 91 | return xxx_messageInfo_DummyMessage.Size(m) 92 | } 93 | func (m *DummyMessage) XXX_DiscardUnknown() { 94 | xxx_messageInfo_DummyMessage.DiscardUnknown(m) 95 | } 96 | 97 | var xxx_messageInfo_DummyMessage proto.InternalMessageInfo 98 | 99 | func (m *DummyMessage) GetId() int32 { 100 | if m != nil { 101 | return m.Id 102 | } 103 | return 0 104 | } 105 | 106 | func (m *DummyMessage) GetName() string { 107 | if m != nil { 108 | return m.Name 109 | } 110 | return "" 111 | } 112 | 113 | func init() { 114 | proto.RegisterType((*ComplexMessage)(nil), "example.complex.ComplexMessage") 115 | proto.RegisterType((*DummyMessage)(nil), "example.complex.DummyMessage") 116 | } 117 | 118 | func init() { proto.RegisterFile("complex/complex.proto", fileDescriptor_complex_a33d9ec46b6da591) } 119 | 120 | var fileDescriptor_complex_a33d9ec46b6da591 = []byte{ 121 | // 176 bytes of a gzipped FileDescriptorProto 122 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4d, 0xce, 0xcf, 0x2d, 123 | 0xc8, 0x49, 0xad, 0xd0, 0x87, 0xd2, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0xfc, 0xa9, 0x15, 124 | 0x89, 0x20, 0xbe, 0x1e, 0x54, 0x58, 0x69, 0x12, 0x23, 0x17, 0x9f, 0x33, 0x84, 0xed, 0x9b, 0x5a, 125 | 0x5c, 0x9c, 0x98, 0x9e, 0x2a, 0x64, 0xc5, 0xc5, 0x99, 0x9f, 0x97, 0x1a, 0x9f, 0x52, 0x9a, 0x9b, 126 | 0x5b, 0x29, 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0x6d, 0x24, 0xab, 0x87, 0xa6, 0x4f, 0xcf, 0x05, 0x24, 127 | 0x0b, 0xd5, 0x11, 0xc4, 0x91, 0x9f, 0x97, 0x0a, 0x16, 0x10, 0x72, 0xe1, 0xe2, 0xcb, 0x2d, 0xcd, 128 | 0x29, 0xc9, 0x2c, 0xc8, 0x81, 0x19, 0xc0, 0xac, 0xc0, 0x4c, 0xd8, 0x00, 0x5e, 0x98, 0x26, 0xb0, 129 | 0xa8, 0x92, 0x11, 0x17, 0x0f, 0xb2, 0xb4, 0x10, 0x1f, 0x17, 0x53, 0x66, 0x8a, 0x04, 0xa3, 0x02, 130 | 0xa3, 0x06, 0x6b, 0x10, 0x53, 0x66, 0x8a, 0x90, 0x10, 0x17, 0x4b, 0x5e, 0x62, 0x6e, 0x2a, 0xd8, 131 | 0x71, 0x9c, 0x41, 0x60, 0xb6, 0x13, 0x77, 0x14, 0x27, 0xd4, 0xe8, 0x82, 0xa4, 0x24, 0x36, 0xb0, 132 | 0x6f, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x2f, 0x59, 0x14, 0x8b, 0x06, 0x01, 0x00, 0x00, 133 | } 134 | -------------------------------------------------------------------------------- /src/complex/complex.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package example.complex; 4 | 5 | option go_package = "complexpb"; 6 | 7 | message ComplexMessage { 8 | DummyMessage one_dummy = 2; 9 | repeated DummyMessage multiple_dummy = 3; 10 | } 11 | 12 | message DummyMessage { 13 | int32 id = 1; 14 | string name = 2; 15 | } -------------------------------------------------------------------------------- /src/enum_example/enum_example.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: enum_example/enum_example.proto 3 | 4 | package enumpb 5 | 6 | import proto "github.com/golang/protobuf/proto" 7 | import fmt "fmt" 8 | import math "math" 9 | 10 | // Reference imports to suppress errors if they are not otherwise used. 11 | var _ = proto.Marshal 12 | var _ = fmt.Errorf 13 | var _ = math.Inf 14 | 15 | // This is a compile-time assertion to ensure that this generated file 16 | // is compatible with the proto package it is being compiled against. 17 | // A compilation error at this line likely means your copy of the 18 | // proto package needs to be updated. 19 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 20 | 21 | type DayOfTheWeek int32 22 | 23 | const ( 24 | DayOfTheWeek_UNKNOWN DayOfTheWeek = 0 25 | DayOfTheWeek_MONDAY DayOfTheWeek = 1 26 | DayOfTheWeek_TUESDAY DayOfTheWeek = 2 27 | DayOfTheWeek_WEDNESDAY DayOfTheWeek = 3 28 | DayOfTheWeek_THURSDAY DayOfTheWeek = 4 29 | DayOfTheWeek_FRIDAY DayOfTheWeek = 5 30 | DayOfTheWeek_SATURDAY DayOfTheWeek = 6 31 | DayOfTheWeek_SUNDAY DayOfTheWeek = 7 32 | ) 33 | 34 | var DayOfTheWeek_name = map[int32]string{ 35 | 0: "UNKNOWN", 36 | 1: "MONDAY", 37 | 2: "TUESDAY", 38 | 3: "WEDNESDAY", 39 | 4: "THURSDAY", 40 | 5: "FRIDAY", 41 | 6: "SATURDAY", 42 | 7: "SUNDAY", 43 | } 44 | var DayOfTheWeek_value = map[string]int32{ 45 | "UNKNOWN": 0, 46 | "MONDAY": 1, 47 | "TUESDAY": 2, 48 | "WEDNESDAY": 3, 49 | "THURSDAY": 4, 50 | "FRIDAY": 5, 51 | "SATURDAY": 6, 52 | "SUNDAY": 7, 53 | } 54 | 55 | func (x DayOfTheWeek) String() string { 56 | return proto.EnumName(DayOfTheWeek_name, int32(x)) 57 | } 58 | func (DayOfTheWeek) EnumDescriptor() ([]byte, []int) { 59 | return fileDescriptor_enum_example_170da7e58fafaec6, []int{0} 60 | } 61 | 62 | type EnumMessage struct { 63 | Id int32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"` 64 | DayOfTheWeek DayOfTheWeek `protobuf:"varint,2,opt,name=day_of_the_week,json=dayOfTheWeek,enum=example.enumerations.DayOfTheWeek" json:"day_of_the_week,omitempty"` 65 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 66 | XXX_unrecognized []byte `json:"-"` 67 | XXX_sizecache int32 `json:"-"` 68 | } 69 | 70 | func (m *EnumMessage) Reset() { *m = EnumMessage{} } 71 | func (m *EnumMessage) String() string { return proto.CompactTextString(m) } 72 | func (*EnumMessage) ProtoMessage() {} 73 | func (*EnumMessage) Descriptor() ([]byte, []int) { 74 | return fileDescriptor_enum_example_170da7e58fafaec6, []int{0} 75 | } 76 | func (m *EnumMessage) XXX_Unmarshal(b []byte) error { 77 | return xxx_messageInfo_EnumMessage.Unmarshal(m, b) 78 | } 79 | func (m *EnumMessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 80 | return xxx_messageInfo_EnumMessage.Marshal(b, m, deterministic) 81 | } 82 | func (dst *EnumMessage) XXX_Merge(src proto.Message) { 83 | xxx_messageInfo_EnumMessage.Merge(dst, src) 84 | } 85 | func (m *EnumMessage) XXX_Size() int { 86 | return xxx_messageInfo_EnumMessage.Size(m) 87 | } 88 | func (m *EnumMessage) XXX_DiscardUnknown() { 89 | xxx_messageInfo_EnumMessage.DiscardUnknown(m) 90 | } 91 | 92 | var xxx_messageInfo_EnumMessage proto.InternalMessageInfo 93 | 94 | func (m *EnumMessage) GetId() int32 { 95 | if m != nil { 96 | return m.Id 97 | } 98 | return 0 99 | } 100 | 101 | func (m *EnumMessage) GetDayOfTheWeek() DayOfTheWeek { 102 | if m != nil { 103 | return m.DayOfTheWeek 104 | } 105 | return DayOfTheWeek_UNKNOWN 106 | } 107 | 108 | func init() { 109 | proto.RegisterType((*EnumMessage)(nil), "example.enumerations.EnumMessage") 110 | proto.RegisterEnum("example.enumerations.DayOfTheWeek", DayOfTheWeek_name, DayOfTheWeek_value) 111 | } 112 | 113 | func init() { 114 | proto.RegisterFile("enum_example/enum_example.proto", fileDescriptor_enum_example_170da7e58fafaec6) 115 | } 116 | 117 | var fileDescriptor_enum_example_170da7e58fafaec6 = []byte{ 118 | // 239 bytes of a gzipped FileDescriptorProto 119 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4f, 0xcd, 0x2b, 0xcd, 120 | 0x8d, 0x4f, 0xad, 0x48, 0xcc, 0x2d, 0xc8, 0x49, 0xd5, 0x47, 0xe6, 0xe8, 0x15, 0x14, 0xe5, 0x97, 121 | 0xe4, 0x0b, 0x89, 0xc0, 0xb8, 0x20, 0xb9, 0xd4, 0xa2, 0xc4, 0x92, 0xcc, 0xfc, 0xbc, 0x62, 0xa5, 122 | 0x0c, 0x2e, 0x6e, 0xd7, 0xbc, 0xd2, 0x5c, 0xdf, 0xd4, 0xe2, 0xe2, 0xc4, 0xf4, 0x54, 0x21, 0x3e, 123 | 0x2e, 0xa6, 0xcc, 0x14, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xd6, 0x20, 0xa6, 0xcc, 0x14, 0x21, 0x4f, 124 | 0x2e, 0xfe, 0x94, 0xc4, 0xca, 0xf8, 0xfc, 0xb4, 0xf8, 0x92, 0x8c, 0xd4, 0xf8, 0xf2, 0xd4, 0xd4, 125 | 0x6c, 0x09, 0x26, 0x05, 0x46, 0x0d, 0x3e, 0x23, 0x25, 0x3d, 0x6c, 0xc6, 0xe9, 0xb9, 0x24, 0x56, 126 | 0xfa, 0xa7, 0x85, 0x64, 0xa4, 0x86, 0xa7, 0xa6, 0x66, 0x07, 0xf1, 0xa4, 0x20, 0xf1, 0xb4, 0xca, 127 | 0xb9, 0x78, 0x90, 0x65, 0x85, 0xb8, 0xb9, 0xd8, 0x43, 0xfd, 0xbc, 0xfd, 0xfc, 0xc3, 0xfd, 0x04, 128 | 0x18, 0x84, 0xb8, 0xb8, 0xd8, 0x7c, 0xfd, 0xfd, 0x5c, 0x1c, 0x23, 0x05, 0x18, 0x41, 0x12, 0x21, 129 | 0xa1, 0xae, 0xc1, 0x20, 0x0e, 0x93, 0x10, 0x2f, 0x17, 0x67, 0xb8, 0xab, 0x8b, 0x1f, 0x84, 0xcb, 130 | 0x2c, 0xc4, 0xc3, 0xc5, 0x11, 0xe2, 0x11, 0x1a, 0x04, 0xe6, 0xb1, 0x80, 0x74, 0xb9, 0x05, 0x79, 131 | 0x82, 0xd8, 0xac, 0x20, 0x99, 0x60, 0xc7, 0x90, 0xd0, 0x20, 0x10, 0x8f, 0x0d, 0x24, 0x13, 0x1c, 132 | 0x0a, 0x36, 0x8f, 0xdd, 0x89, 0x23, 0x8a, 0x0d, 0xe4, 0xc6, 0x82, 0xa4, 0x24, 0x36, 0x70, 0x48, 133 | 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x32, 0x5e, 0x13, 0x6a, 0x2c, 0x01, 0x00, 0x00, 134 | } 135 | -------------------------------------------------------------------------------- /src/enum_example/enum_example.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package example.enumerations; 4 | 5 | option go_package = "enumpb"; 6 | 7 | message EnumMessage { 8 | int32 id = 1; 9 | DayOfTheWeek day_of_the_week = 2; 10 | } 11 | 12 | enum DayOfTheWeek { 13 | UNKNOWN = 0; 14 | MONDAY = 1; 15 | TUESDAY = 2; 16 | WEDNESDAY = 3; 17 | THURSDAY = 4; 18 | FRIDAY = 5; 19 | SATURDAY = 6; 20 | SUNDAY = 7; 21 | } 22 | -------------------------------------------------------------------------------- /src/simple/simple.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: simple/simple.proto 3 | 4 | package simplepb 5 | 6 | import proto "github.com/golang/protobuf/proto" 7 | import fmt "fmt" 8 | import math "math" 9 | 10 | // Reference imports to suppress errors if they are not otherwise used. 11 | var _ = proto.Marshal 12 | var _ = fmt.Errorf 13 | var _ = math.Inf 14 | 15 | // This is a compile-time assertion to ensure that this generated file 16 | // is compatible with the proto package it is being compiled against. 17 | // A compilation error at this line likely means your copy of the 18 | // proto package needs to be updated. 19 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 20 | 21 | type SimpleMessage struct { 22 | Id int32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"` 23 | IsSimple bool `protobuf:"varint,2,opt,name=is_simple,json=isSimple" json:"is_simple,omitempty"` 24 | Name string `protobuf:"bytes,3,opt,name=name" json:"name,omitempty"` 25 | SampleList []int32 `protobuf:"varint,4,rep,packed,name=sample_list,json=sampleList" json:"sample_list,omitempty"` 26 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 27 | XXX_unrecognized []byte `json:"-"` 28 | XXX_sizecache int32 `json:"-"` 29 | } 30 | 31 | func (m *SimpleMessage) Reset() { *m = SimpleMessage{} } 32 | func (m *SimpleMessage) String() string { return proto.CompactTextString(m) } 33 | func (*SimpleMessage) ProtoMessage() {} 34 | func (*SimpleMessage) Descriptor() ([]byte, []int) { 35 | return fileDescriptor_simple_8128e7a3faf92167, []int{0} 36 | } 37 | func (m *SimpleMessage) XXX_Unmarshal(b []byte) error { 38 | return xxx_messageInfo_SimpleMessage.Unmarshal(m, b) 39 | } 40 | func (m *SimpleMessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 41 | return xxx_messageInfo_SimpleMessage.Marshal(b, m, deterministic) 42 | } 43 | func (dst *SimpleMessage) XXX_Merge(src proto.Message) { 44 | xxx_messageInfo_SimpleMessage.Merge(dst, src) 45 | } 46 | func (m *SimpleMessage) XXX_Size() int { 47 | return xxx_messageInfo_SimpleMessage.Size(m) 48 | } 49 | func (m *SimpleMessage) XXX_DiscardUnknown() { 50 | xxx_messageInfo_SimpleMessage.DiscardUnknown(m) 51 | } 52 | 53 | var xxx_messageInfo_SimpleMessage proto.InternalMessageInfo 54 | 55 | func (m *SimpleMessage) GetId() int32 { 56 | if m != nil { 57 | return m.Id 58 | } 59 | return 0 60 | } 61 | 62 | func (m *SimpleMessage) GetIsSimple() bool { 63 | if m != nil { 64 | return m.IsSimple 65 | } 66 | return false 67 | } 68 | 69 | func (m *SimpleMessage) GetName() string { 70 | if m != nil { 71 | return m.Name 72 | } 73 | return "" 74 | } 75 | 76 | func (m *SimpleMessage) GetSampleList() []int32 { 77 | if m != nil { 78 | return m.SampleList 79 | } 80 | return nil 81 | } 82 | 83 | func init() { 84 | proto.RegisterType((*SimpleMessage)(nil), "example.simple.SimpleMessage") 85 | } 86 | 87 | func init() { proto.RegisterFile("simple/simple.proto", fileDescriptor_simple_8128e7a3faf92167) } 88 | 89 | var fileDescriptor_simple_8128e7a3faf92167 = []byte{ 90 | // 156 bytes of a gzipped FileDescriptorProto 91 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0xce, 0xcc, 0x2d, 92 | 0xc8, 0x49, 0xd5, 0x87, 0x50, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0x7c, 0xa9, 0x15, 0x89, 93 | 0x60, 0x2e, 0x44, 0x54, 0xa9, 0x90, 0x8b, 0x37, 0x18, 0xcc, 0xf2, 0x4d, 0x2d, 0x2e, 0x4e, 0x4c, 94 | 0x4f, 0x15, 0xe2, 0xe3, 0x62, 0xca, 0x4c, 0x91, 0x60, 0x54, 0x60, 0xd4, 0x60, 0x0d, 0x62, 0xca, 95 | 0x4c, 0x11, 0x92, 0xe6, 0xe2, 0xcc, 0x2c, 0x8e, 0x87, 0xa8, 0x96, 0x60, 0x52, 0x60, 0xd4, 0xe0, 96 | 0x08, 0xe2, 0xc8, 0x2c, 0x86, 0xe8, 0x11, 0x12, 0xe2, 0x62, 0xc9, 0x4b, 0xcc, 0x4d, 0x95, 0x60, 97 | 0x56, 0x60, 0xd4, 0xe0, 0x0c, 0x02, 0xb3, 0x85, 0xe4, 0xb9, 0xb8, 0x8b, 0xc1, 0x56, 0xc4, 0xe7, 98 | 0x64, 0x16, 0x97, 0x48, 0xb0, 0x28, 0x30, 0x6b, 0xb0, 0x06, 0x71, 0x41, 0x84, 0x7c, 0x32, 0x8b, 99 | 0x4b, 0x9c, 0xb8, 0xa2, 0x38, 0x20, 0xc6, 0x15, 0x24, 0x25, 0xb1, 0x81, 0x5d, 0x65, 0x0c, 0x08, 100 | 0x00, 0x00, 0xff, 0xff, 0xea, 0x70, 0x2d, 0xce, 0xac, 0x00, 0x00, 0x00, 101 | } 102 | -------------------------------------------------------------------------------- /src/simple/simple.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package example.simple; 4 | 5 | option go_package = "simplepb"; 6 | 7 | message SimpleMessage { 8 | int32 id = 1; 9 | bool is_simple = 2; 10 | string name = 3; 11 | repeated int32 sample_list = 4; 12 | } --------------------------------------------------------------------------------