go 使用protobuffer proto3

1、获取 Protobuf 编译器 protoc,跟C++通用的;可以在git下载到二进制文件


2、获取 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go

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

将protoc-gen-go二进制执行程序所在的目录加入到环境变量,或者直接将二进制文件拷贝到 protoc 所在的目录


3.获取 goprotobuf 提供的支持库,包含诸如编码(marshaling)、解码(unmarshaling)等功能

go get github.com/golang/protobuf/proto


4、测试协议 msg.proto:

syntax = "proto3";
package Im;   


message helloworld   
{   
     int32     id = 1;  // ID     
     string    str = 2;  // str    
     int32     opt = 3;  //optional field   
}

5、协议文件得到go文件

protoc --go_out=. msg.proto


msg.pb.go 的内容如下(可以看出proto2中原有的每个字段读取的方法去掉了,现在proto3是直接访问):

// Code generated by protoc-gen-go.
// source: msg.proto
// DO NOT EDIT!

/*
Package Im is a generated protocol buffer package.

It is generated from these files:
	msg.proto

It has these top-level messages:
	Helloworld
*/
package Im

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import 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.ProtoPackageIsVersion2 // please upgrade the proto package

type Helloworld struct {
	Id  int32  `protobuf:"varint,1,opt,name=id" json:"id,omitempty"`
	Str string `protobuf:"bytes,2,opt,name=str" json:"str,omitempty"`
	Opt int32  `protobuf:"varint,3,opt,name=opt" json:"opt,omitempty"`
}

func (m *Helloworld) Reset()                    { *m = Helloworld{} }
func (m *Helloworld) String() string            { return proto.CompactTextString(m) }
func (*Helloworld) ProtoMessage()               {}
func (*Helloworld) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }

func init() {
	proto.RegisterType((*Helloworld)(nil), "Im.helloworld")
}

func init() { proto.RegisterFile("msg.proto", fileDescriptor0) }

var fileDescriptor0 = []byte{
	// 92 bytes of a gzipped FileDescriptorProto
	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0xcc, 0x2d, 0x4e, 0xd7,
	0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xf2, 0xcc, 0x55, 0x32, 0xe2, 0xe2, 0xca, 0x48, 0xcd,
	0xc9, 0xc9, 0x2f, 0xcf, 0x2f, 0xca, 0x49, 0x11, 0xe2, 0xe2, 0x62, 0xca, 0x4c, 0x91, 0x60, 0x54,
	0x60, 0xd4, 0x60, 0x15, 0xe2, 0xe6, 0x62, 0x2e, 0x2e, 0x29, 0x92, 0x60, 0x52, 0x60, 0xd4, 0xe0,
	0x04, 0x71, 0xf2, 0x0b, 0x4a, 0x24, 0x98, 0x41, 0x32, 0x49, 0x6c, 0x60, 0xed, 0xc6, 0x80, 0x00,
	0x00, 0x00, 0xff, 0xff, 0xc8, 0x9e, 0x4a, 0x79, 0x4b, 0x00, 0x00, 0x00,
}



6、文件入库

方案A,入当前工程,使用 import  "./Im"引用:

当前工程目录下新建一个Im的目录(因为协议package 名叫Im)

将生成的msg.pb.go 文件拷贝到此Im目录中


方案B,入go的pkg库,使用  import  "Im"引用:

到go的pkg/src目录下新建一个Im的目录(因为协议package 名叫Im)

将生成的msg.pb.go 文件拷贝到此Im目录中


7、

go测试代码 mainpb.go:

package main

import (
    "log"
    // 辅助库
    "github.com/golang/protobuf/proto"
    // x.pb.go 的路径
    "./Im"
)

func main() {
    // 创建一个消息 
     test := &Im.Helloworld{
        // 使用辅助函数设置域的值
        Str: "hello!" ,
      //  Id:  321,
        Opt: 1234,
    }

    test.Id = 3244

    // 进行编码
    data, err := proto.Marshal(test)
    if err != nil {
        log.Fatal("marshaling error: ", err)
    }

    // 进行解码
    newTest := &Im.Helloworld{}
    err = proto.Unmarshal(data, newTest)
    if err != nil {
        log.Fatal("unmarshaling error: ", err)
    }

    log.Printf("id:%d;opt:%d;str:%s;",newTest.Id,newTest.Opt,newTest.Str)

    // 测试结果
    if test.String() != newTest.String() {
        log.Fatalf("data mismatch %q != %q", test.String(), newTest.String())
    }
}


go run mainpb.go

运行结果:
2016/09/08 16:27:52 id:321;opt:1234;str:hello!;


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值