下载安装
1、源代码下载
https://github.com/protocolbuffers/protobuf/releases
2、将解压后的protoc.exe文件放到$GoPath$/bin目录下
3、配置环境变量,将 F:\GoPath\bin 添加到Path路径后面
4、
go get -u github.com/golang/protobuf/protoc-gen-go
编写代码
1、创建一个Go工程
2、创建一个test.proto文件
syntax = "proto3"; //版本
package pro;
enum PhoneType {
HOME = 0;
WORK = 1;
}
message Phone{ //每个message名都要大写
PhoneType type = 1;
string number = 2;
}
message Person{
int32 id = 1;
string name = 2;
repeated Phone phones = 3; //定义数组类型
}
message ContactBook{
repeated Person persons = 1;
}
3、在该文件目录下,使用命令行 (go_out为输出路径,中间空格,后面跟文件名)
protoc --go_out=. test.proto
4、得到编译后生成的go文件
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: test.proto
package pro
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type PhoneType int32
const (
PhoneType_HOME PhoneType = 0
PhoneType_WORK PhoneType = 1
)
var PhoneType_name = map[int32]string{
0: "HOME",
1: "WORK",
}
var PhoneType_value = map[string]int32{
"HOME": 0,
"WORK": 1,
}
func (x PhoneType) String() string {
return proto.EnumName(PhoneType_name, int32(x))
}
func (PhoneType) EnumDescriptor() ([]byte, []int) {
return fileDescriptor_c161fcfdc0c3ff1e, []int{0}
}
type Phone struct {
Type PhoneType `protobuf:"varint,1,opt,name=type,proto3,enum=pro.PhoneType" json:"type,omitempty"`
Number string `protobuf:"bytes,2,opt,name=number,proto3" json:"number,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Phone) Reset() { *m = Phone{} }
func (m *Phone) String() string { return proto.CompactTextString(m) }
func (*Phone) ProtoMessage() {}
func (*Phone) Descriptor() ([]byte, []int) {
return fileDescriptor_c161fcfdc0c3ff1e, []int{0}
}
func (m *Phone) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Phone.Unmarshal(m, b)
}
func (m *Phone) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Phone.Marshal(b, m, deterministic)
}
func (m *Phone) XXX_Merge(src proto.Message) {
xxx_messageInfo_Phone.Merge(m, src)
}
func (m *Phone) XXX_Size() int {
return xxx_messageInfo_Phone.Size(m)
}
func (m *Phone) XXX_DiscardUnknown() {
xxx_messageInfo_Phone.DiscardUnknown(m)
}
var xxx_messageInfo_Phone proto.InternalMessageInfo
func (m *Phone) GetType() PhoneType {
if m != nil {
return m.Type
}
return PhoneType_HOME
}
func (m *Phone) GetNumber() string {
if m != nil {
return m.Number
}
return ""
}
type Person struct {
Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Phones []*Phone `protobuf:"bytes,3,rep,name=phones,proto3" json:"phones,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Person) Reset() { *m = Person{} }
func (m *Person) String() string { return proto.CompactTextString(m) }
func (*Person) ProtoMessage() {}
func (*Person) Descriptor() ([]byte, []int) {
return fileDescriptor_c161fcfdc0c3ff1e, []int{1}
}
func (m *Person) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Person.Unmarshal(m, b)
}
func (m *Person) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Person.Marshal(b, m, deterministic)
}
func (m *Person) XXX_Merge(src proto.Message) {
xxx_messageInfo_Person.Merge(m, src)
}
func (m *Person) XXX_Size() int {
return xxx_messageInfo_Person.Size(m)
}
func (m *Person) XXX_DiscardUnknown() {
xxx_messageInfo_Person.DiscardUnknown(m)
}
var xxx_messageInfo_Person proto.InternalMessageInfo
func (m *Person) GetId() int32 {
if m != nil {
return m.Id
}
return 0
}
func (m *Person) GetName() string {
if m != nil {
return m.Name
}
return ""
}
func (m *Person) GetPhones() []*Phone {
if m != nil {
return m.Phones
}
return nil
}
type ContactBook struct {
Persons []*Person `protobuf:"bytes,1,rep,name=persons,proto3" json:"persons,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *ContactBook) Reset() { *m = ContactBook{} }
func (m *ContactBook) String() string { return proto.CompactTextString(m) }
func (*ContactBook) ProtoMessage() {}
func (*ContactBook) Descriptor() ([]byte, []int) {
return fileDescriptor_c161fcfdc0c3ff1e, []int{2}
}
func (m *ContactBook) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_ContactBook.Unmarshal(m, b)
}
func (m *ContactBook) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_ContactBook.Marshal(b, m, deterministic)
}
func (m *ContactBook) XXX_Merge(src proto.Message) {
xxx_messageInfo_ContactBook.Merge(m, src)
}
func (m *ContactBook) XXX_Size() int {
return xxx_messageInfo_ContactBook.Size(m)
}
func (m *ContactBook) XXX_DiscardUnknown() {
xxx_messageInfo_ContactBook.DiscardUnknown(m)
}
var xxx_messageInfo_ContactBook proto.InternalMessageInfo
func (m *ContactBook) GetPersons() []*Person {
if m != nil {
return m.Persons
}
return nil
}
func init() {
proto.RegisterEnum("pro.PhoneType", PhoneType_name, PhoneType_value)
proto.RegisterType((*Phone)(nil), "pro.Phone")
proto.RegisterType((*Person)(nil), "pro.Person")
proto.RegisterType((*ContactBook)(nil), "pro.ContactBook")
}
func init() { proto.RegisterFile("test.proto", fileDescriptor_c161fcfdc0c3ff1e) }
var fileDescriptor_c161fcfdc0c3ff1e = []byte{
// 213 bytes of a gzipped FileDescriptorProto
0x1f, 0x8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0xa7, 0xdd, 0xc5, 0xa2, 0xde, 0x4f,
0xe6, 0x65, 0x27, 0x7a, 0xc9, 0x3f, 0xbf, 0xff, 0x09, 0x00, 0x00, 0xff, 0xff, 0x25, 0x00, 0x9c,
0x6d, 0x07, 0x01, 0x00, 0x00,
}
5、编写main
package main
import (
"fmt"
"github.com/golang/protobuf/proto"
"io/ioutil"
"os"
"protobuf/pro"
)
func write() {
p1 := &pro.Person{ //实例对象要&
Id:1,
Name:"张三",
Phones:[]*pro.Phone{ //数组前面要加*
{
Type:pro.PhoneType_HOME, //赋值是key : value
Number:"123",
},
{
Type:pro.PhoneType_WORK,
Number:"321",
},
},
}
book := &pro.ContactBook{}
book.Persons = append(book.Persons,p1)
data,_ := proto.Marshal(book)
ioutil.WriteFile("./test.txt",data,os.ModePerm)
}
func read() {
data,_ := ioutil.ReadFile("./test.txt");
book := &pro.ContactBook{};
proto.Unmarshal(data,book);
for _,v := range book.Persons{
fmt.Println(v.Id,v.Name)
for _,vv := range v.Phones{
fmt.Println(vv.Type,vv.Number)
}
}
}
func main() {
write()
read()
}
6、运行
1 张三
HOME 123
WORK 321
Process finished with exit code 0