golang 在windows下使用protobuf的一次完整记录

 

 

 

1、安装protobuf库文件

go get github.com/golang/protobuf/proto

2、安装protobuf的编译器

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

2.2 使用 gogoprotobuf完全兼容google protobuf,它生成的代码质量和编解码性能均比goprotobuf高一些。以下a、b二选一即可

a、gogo

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

b、gofast

go get github.com/gogo/protobuf/protoc-gen-gofast

成功后会在GOPATH的第一个目录的bin文件夹下生成protoc-gen-go可执行文件(如果该路径没在系统的环境变量中,则需要将改路径也加入到环境变量的path中去)。

注:如果是手动下载的gofast、gogo等文件包,需要切换到响应的目录go build  生成可执行文件,并移动到你的bin文件夹下

3、使用protoc编译.proto文件成.pd.go文件

a、protoc --go_out=. *.proto

b、protoc --gofast_out=plugins=grpc:. test.proto

示例 test.proto

syntax = "proto3";

package jlb;

message Request{
    string req_id=1;
    repeated int64 a_ids=2;
    string platform=3;
    int64 ts=4;
}

 

编译后的go文件test.pb.go

// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: test.proto

package jlb

import (
   fmt "fmt"
   proto "github.com/golang/protobuf/proto"
   io "io"
   math "math"
   math_bits "math/bits"
)

// 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 Request struct {
   ReqId                string   `protobuf:"bytes,1,opt,name=req_id,json=reqId,proto3" json:"req_id,omitempty"`
   AIds                 []int64  `protobuf:"varint,2,rep,packed,name=a_ids,json=aIds,proto3" json:"a_ids,omitempty"`
   Platform             string   `protobuf:"bytes,3,opt,name=platform,proto3" json:"platform,omitempty"`
   Ts                   int64    `protobuf:"varint,4,opt,name=ts,proto3" json:"ts,omitempty"`
   XXX_NoUnkeyedLiteral struct{} `json:"-"`
   XXX_unrecognized     []byte   `json:"-"`
   XXX_sizecache        int32    `json:"-"`
}

func (m *Request) Reset()         { *m = Request{} }
func (m *Request) String() string { return proto.CompactTextString(m) }
func (*Request) ProtoMessage()    {}
func (*Request) Descriptor() ([]byte, []int) {
   return fileDescriptor_c161fcfdc0c3ff1e, []int{0}
}
func (m *Request) XXX_Unmarshal(b []byte) error {
   return m.Unmarshal(b)
}
func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
   if deterministic {
      return xxx_messageInfo_Request.Marshal(b, m, deterministic)
   } else {
      b = b[:cap(b)]
      n, err := m.MarshalToSizedBuffer(b)
      if err != nil {
         return nil, err
      }
      return b[:n], nil
   }
}
func (m *Request) XXX_Merge(src proto.Message) {
   xxx_messageInfo_Request.Merge(m, src)
}
func (m *Request) XXX_Size() int {
   return m.Size()
}
func (m *Request) XXX_DiscardUnknown() {
   xxx_messageInfo_Request.DiscardUnknown(m)
}

var xxx_messageInfo_Request proto.InternalMessageInfo

func (m *Request) GetReqId() string {
   if m != nil {
      return m.ReqId
   }
   return ""
}

func (m *Request) GetAIds() []int64 {
   if m != nil {
      return m.AIds
   }
   return nil
}

func (m *Request) GetPlatform() string {
   if m != nil {
      return m.Platform
   }
   return ""
}

func (m *Request) GetTs() int64 {
   if m != nil {
      return m.Ts
   }
   return 0
}

func init() {
   proto.RegisterType((*Request)(nil), "jlb.Request")
}

func init() { proto.RegisterFile("test.proto", fileDescriptor_c161fcfdc0c3ff1e) }

var fileDescriptor_c161fcfdc0c3ff1e = []byte{
   // 154 bytes of a gzipped FileDescriptorProto
   0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2a, 0x49, 0x2d, 0x2e,
   0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xce, 0xca, 0x49, 0x52, 0x4a, 0xe4, 0x62, 0x0f,
   0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x11, 0x12, 0xe5, 0x62, 0x2b, 0x4a, 0x2d, 0x8c, 0xcf, 0x4c,
   0x91, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x62, 0x2d, 0x4a, 0x2d, 0xf4, 0x4c, 0x11, 0x12, 0xe6,
   0x62, 0x4d, 0x8c, 0xcf, 0x4c, 0x29, 0x96, 0x60, 0x52, 0x60, 0xd6, 0x60, 0x0e, 0x62, 0x49, 0xf4,
   0x4c, 0x29, 0x16, 0x92, 0xe2, 0xe2, 0x28, 0xc8, 0x49, 0x2c, 0x49, 0xcb, 0x2f, 0xca, 0x95, 0x60,
   0x06, 0xab, 0x86, 0xf3, 0x85, 0xf8, 0xb8, 0x98, 0x4a, 0x8a, 0x25, 0x58, 0x14, 0x18, 0x35, 0x98,
   0x83, 0x98, 0x4a, 0x8a, 0x9d, 0x04, 0x4e, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1,
   0x23, 0x39, 0xc6, 0x19, 0x8f, 0xe5, 0x18, 0x92, 0xd8, 0xc0, 0x0e, 0x30, 0x06, 0x04, 0x00, 0x00,
   0xff, 0xff, 0x73, 0x6a, 0x49, 0x0d, 0x8e, 0x00, 0x00, 0x00,
}

func (m *Request) Marshal() (dAtA []byte, err error) {
   size := m.Size()
   dAtA = make([]byte, size)
   n, err := m.MarshalToSizedBuffer(dAtA[:size])
   if err != nil {
      return nil, err
   }
   return dAtA[:n], nil
}

func (m *Request) MarshalTo(dAtA []byte) (int, error) {
   size := m.Size()
   return m.MarshalToSizedBuffer(dAtA[:size])
}

func (m *Request) MarshalToSizedBuffer(dAtA []byte) (int, error) {
   i := len(dAtA)
   _ = i
   var l int
   _ = l
   if m.XXX_unrecognized != nil {
      i -= len(m.XXX_unrecognized)
      copy(dAtA[i:], m.XXX_unrecognized)
   }
   if m.Ts != 0 {
      i = encodeVarintTest(dAtA, i, uint64(m.Ts))
      i--
      dAtA[i] = 0x20
   }
   if len(m.Platform) > 0 {
      i -= len(m.Platform)
      copy(dAtA[i:], m.Platform)
      i = encodeVarintTest(dAtA, i, uint64(len(m.Platform)))
      i--
      dAtA[i] = 0x1a
   }
   if len(m.AIds) > 0 {
      dAtA2 := make([]byte, len(m.AIds)*10)
      var j1 int
      for _, num1 := range m.AIds {
         num := uint64(num1)
         for num >= 1<<7 {
            dAtA2[j1] = uint8(uint64(num)&0x7f | 0x80)
            num >>= 7
            j1++
         }
         dAtA2[j1] = uint8(num)
         j1++
      }
      i -= j1
      copy(dAtA[i:], dAtA2[:j1])
      i = encodeVarintTest(dAtA, i, uint64(j1))
      i--
      dAtA[i] = 0x12
   }
   if len(m.ReqId) > 0 {
      i -= len(m.ReqId)
      copy(dAtA[i:], m.ReqId)
      i = encodeVarintTest(dAtA, i, uint64(len(m.ReqId)))
      i--
      dAtA[i] = 0xa
   }
   return len(dAtA) - i, nil
}

func encodeVarintTest(dAtA []byte, offset int, v uint64) int {
   offset -= sovTest(v)
   base := offset
   for v >= 1<<7 {
      dAtA[offset] = uint8(v&0x7f | 0x80)
      v >>= 7
      offset++
   }
   dAtA[offset] = uint8(v)
   return base
}
func (m *Request) Size() (n int) {
   if m == nil {
      return 0
   }
   var l int
   _ = l
   l = len(m.ReqId)
   if l > 0 {
      n += 1 + l + sovTest(uint64(l))
   }
   if len(m.AIds) > 0 {
      l = 0
      for _, e := range m.AIds {
         l += sovTest(uint64(e))
      }
      n += 1 + sovTest(uint64(l)) + l
   }
   l = len(m.Platform)
   if l > 0 {
      n += 1 + l + sovTest(uint64(l))
   }
   if m.Ts != 0 {
      n += 1 + sovTest(uint64(m.Ts))
   }
   if m.XXX_unrecognized != nil {
      n += len(m.XXX_unrecognized)
   }
   return n
}

func sovTest(x uint64) (n int) {
   return (math_bits.Len64(x|1) + 6) / 7
}
func sozTest(x uint64) (n int) {
   return sovTest(uint64((x << 1) ^ uint64((int64(x) >> 63))))
}
func (m *Request) Unmarshal(dAtA []byte) error {
   l := len(dAtA)
   iNdEx := 0
   for iNdEx < l {
      preIndex := iNdEx
      var wire uint64
      for shift := uint(0); ; shift += 7 {
         if shift >= 64 {
            return ErrIntOverflowTest
         }
         if iNdEx >= l {
            return io.ErrUnexpectedEOF
         }
         b := dAtA[iNdEx]
         iNdEx++
         wire |= uint64(b&0x7F) << shift
         if b < 0x80 {
            break
         }
      }
      fieldNum := int32(wire >> 3)
      wireType := int(wire & 0x7)
      if wireType == 4 {
         return fmt.Errorf("proto: Request: wiretype end group for non-group")
      }
      if fieldNum <= 0 {
         return fmt.Errorf("proto: Request: illegal tag %d (wire type %d)", fieldNum, wire)
      }
      switch fieldNum {
      case 1:
         if wireType != 2 {
            return fmt.Errorf("proto: wrong wireType = %d for field ReqId", wireType)
         }
         var stringLen uint64
         for shift := uint(0); ; shift += 7 {
            if shift >= 64 {
               return ErrIntOverflowTest
            }
            if iNdEx >= l {
               return io.ErrUnexpectedEOF
            }
            b := dAtA[iNdEx]
            iNdEx++
            stringLen |= uint64(b&0x7F) << shift
            if b < 0x80 {
               break
            }
         }
         intStringLen := int(stringLen)
         if intStringLen < 0 {
            return ErrInvalidLengthTest
         }
         postIndex := iNdEx + intStringLen
         if postIndex < 0 {
            return ErrInvalidLengthTest
         }
         if postIndex > l {
            return io.ErrUnexpectedEOF
         }
         m.ReqId = string(dAtA[iNdEx:postIndex])
         iNdEx = postIndex
      case 2:
         if wireType == 0 {
            var v int64
            for shift := uint(0); ; shift += 7 {
               if shift >= 64 {
                  return ErrIntOverflowTest
               }
               if iNdEx >= l {
                  return io.ErrUnexpectedEOF
               }
               b := dAtA[iNdEx]
               iNdEx++
               v |= int64(b&0x7F) << shift
               if b < 0x80 {
                  break
               }
            }
            m.AIds = append(m.AIds, v)
         } else if wireType == 2 {
            var packedLen int
            for shift := uint(0); ; shift += 7 {
               if shift >= 64 {
                  return ErrIntOverflowTest
               }
               if iNdEx >= l {
                  return io.ErrUnexpectedEOF
               }
               b := dAtA[iNdEx]
               iNdEx++
               packedLen |= int(b&0x7F) << shift
               if b < 0x80 {
                  break
               }
            }
            if packedLen < 0 {
               return ErrInvalidLengthTest
            }
            postIndex := iNdEx + packedLen
            if postIndex < 0 {
               return ErrInvalidLengthTest
            }
            if postIndex > l {
               return io.ErrUnexpectedEOF
            }
            var elementCount int
            var count int
            for _, integer := range dAtA[iNdEx:postIndex] {
               if integer < 128 {
                  count++
               }
            }
            elementCount = count
            if elementCount != 0 && len(m.AIds) == 0 {
               m.AIds = make([]int64, 0, elementCount)
            }
            for iNdEx < postIndex {
               var v int64
               for shift := uint(0); ; shift += 7 {
                  if shift >= 64 {
                     return ErrIntOverflowTest
                  }
                  if iNdEx >= l {
                     return io.ErrUnexpectedEOF
                  }
                  b := dAtA[iNdEx]
                  iNdEx++
                  v |= int64(b&0x7F) << shift
                  if b < 0x80 {
                     break
                  }
               }
               m.AIds = append(m.AIds, v)
            }
         } else {
            return fmt.Errorf("proto: wrong wireType = %d for field AIds", wireType)
         }
      case 3:
         if wireType != 2 {
            return fmt.Errorf("proto: wrong wireType = %d for field Platform", wireType)
         }
         var stringLen uint64
         for shift := uint(0); ; shift += 7 {
            if shift >= 64 {
               return ErrIntOverflowTest
            }
            if iNdEx >= l {
               return io.ErrUnexpectedEOF
            }
            b := dAtA[iNdEx]
            iNdEx++
            stringLen |= uint64(b&0x7F) << shift
            if b < 0x80 {
               break
            }
         }
         intStringLen := int(stringLen)
         if intStringLen < 0 {
            return ErrInvalidLengthTest
         }
         postIndex := iNdEx + intStringLen
         if postIndex < 0 {
            return ErrInvalidLengthTest
         }
         if postIndex > l {
            return io.ErrUnexpectedEOF
         }
         m.Platform = string(dAtA[iNdEx:postIndex])
         iNdEx = postIndex
      case 4:
         if wireType != 0 {
            return fmt.Errorf("proto: wrong wireType = %d for field Ts", wireType)
         }
         m.Ts = 0
         for shift := uint(0); ; shift += 7 {
            if shift >= 64 {
               return ErrIntOverflowTest
            }
            if iNdEx >= l {
               return io.ErrUnexpectedEOF
            }
            b := dAtA[iNdEx]
            iNdEx++
            m.Ts |= int64(b&0x7F) << shift
            if b < 0x80 {
               break
            }
         }
      default:
         iNdEx = preIndex
         skippy, err := skipTest(dAtA[iNdEx:])
         if err != nil {
            return err
         }
         if (skippy < 0) || (iNdEx+skippy) < 0 {
            return ErrInvalidLengthTest
         }
         if (iNdEx + skippy) > l {
            return io.ErrUnexpectedEOF
         }
         m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...)
         iNdEx += skippy
      }
   }

   if iNdEx > l {
      return io.ErrUnexpectedEOF
   }
   return nil
}
func skipTest(dAtA []byte) (n int, err error) {
   l := len(dAtA)
   iNdEx := 0
   depth := 0
   for iNdEx < l {
      var wire uint64
      for shift := uint(0); ; shift += 7 {
         if shift >= 64 {
            return 0, ErrIntOverflowTest
         }
         if iNdEx >= l {
            return 0, io.ErrUnexpectedEOF
         }
         b := dAtA[iNdEx]
         iNdEx++
         wire |= (uint64(b) & 0x7F) << shift
         if b < 0x80 {
            break
         }
      }
      wireType := int(wire & 0x7)
      switch wireType {
      case 0:
         for shift := uint(0); ; shift += 7 {
            if shift >= 64 {
               return 0, ErrIntOverflowTest
            }
            if iNdEx >= l {
               return 0, io.ErrUnexpectedEOF
            }
            iNdEx++
            if dAtA[iNdEx-1] < 0x80 {
               break
            }
         }
      case 1:
         iNdEx += 8
      case 2:
         var length int
         for shift := uint(0); ; shift += 7 {
            if shift >= 64 {
               return 0, ErrIntOverflowTest
            }
            if iNdEx >= l {
               return 0, io.ErrUnexpectedEOF
            }
            b := dAtA[iNdEx]
            iNdEx++
            length |= (int(b) & 0x7F) << shift
            if b < 0x80 {
               break
            }
         }
         if length < 0 {
            return 0, ErrInvalidLengthTest
         }
         iNdEx += length
      case 3:
         depth++
      case 4:
         if depth == 0 {
            return 0, ErrUnexpectedEndOfGroupTest
         }
         depth--
      case 5:
         iNdEx += 4
      default:
         return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
      }
      if iNdEx < 0 {
         return 0, ErrInvalidLengthTest
      }
      if depth == 0 {
         return iNdEx, nil
      }
   }
   return 0, io.ErrUnexpectedEOF
}

var (
   ErrInvalidLengthTest        = fmt.Errorf("proto: negative length found during unmarshaling")
   ErrIntOverflowTest          = fmt.Errorf("proto: integer overflow")
   ErrUnexpectedEndOfGroupTest = fmt.Errorf("proto: unexpected end of group")
)

注意:生成的pb.go中结构体json转换忽略零值,如果不忽略,需要手动删除 omitempty;

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值