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

雨点打透心脏的1/2处 2022-10-07 09:50 284阅读 0赞

1、安装protobuf库文件

  1. 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文件夹下

2021061711451731.png

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xjdG1laQ_size_16_color_FFFFFF_t_70

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

a、protoc —go_out=. *.proto

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

示例 test.proto

  1. syntax = "proto3";
  2. package jlb;
  3. message Request{
  4. string req_id=1;
  5. repeated int64 a_ids=2;
  6. string platform=3;
  7. int64 ts=4;
  8. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xjdG1laQ_size_16_color_FFFFFF_t_70 1

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

  1. // Code generated by protoc-gen-gogo. DO NOT EDIT.
  2. // source: test.proto
  3. package jlb
  4. import (
  5. fmt "fmt"
  6. proto "github.com/golang/protobuf/proto"
  7. io "io"
  8. math "math"
  9. math_bits "math/bits"
  10. )
  11. // Reference imports to suppress errors if they are not otherwise used.
  12. var _ = proto.Marshal
  13. var _ = fmt.Errorf
  14. var _ = math.Inf
  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.ProtoPackageIsVersion3 // please upgrade the proto package
  20. type Request struct {
  21. ReqId string `protobuf:"bytes,1,opt,name=req_id,json=reqId,proto3" json:"req_id,omitempty"`
  22. AIds []int64 `protobuf:"varint,2,rep,packed,name=a_ids,json=aIds,proto3" json:"a_ids,omitempty"`
  23. Platform string `protobuf:"bytes,3,opt,name=platform,proto3" json:"platform,omitempty"`
  24. Ts int64 `protobuf:"varint,4,opt,name=ts,proto3" json:"ts,omitempty"`
  25. XXX_NoUnkeyedLiteral struct{} `json:"-"`
  26. XXX_unrecognized []byte `json:"-"`
  27. XXX_sizecache int32 `json:"-"`
  28. }
  29. func (m *Request) Reset() { *m = Request{} }
  30. func (m *Request) String() string { return proto.CompactTextString(m) }
  31. func (*Request) ProtoMessage() {}
  32. func (*Request) Descriptor() ([]byte, []int) {
  33. return fileDescriptor_c161fcfdc0c3ff1e, []int{0}
  34. }
  35. func (m *Request) XXX_Unmarshal(b []byte) error {
  36. return m.Unmarshal(b)
  37. }
  38. func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
  39. if deterministic {
  40. return xxx_messageInfo_Request.Marshal(b, m, deterministic)
  41. } else {
  42. b = b[:cap(b)]
  43. n, err := m.MarshalToSizedBuffer(b)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return b[:n], nil
  48. }
  49. }
  50. func (m *Request) XXX_Merge(src proto.Message) {
  51. xxx_messageInfo_Request.Merge(m, src)
  52. }
  53. func (m *Request) XXX_Size() int {
  54. return m.Size()
  55. }
  56. func (m *Request) XXX_DiscardUnknown() {
  57. xxx_messageInfo_Request.DiscardUnknown(m)
  58. }
  59. var xxx_messageInfo_Request proto.InternalMessageInfo
  60. func (m *Request) GetReqId() string {
  61. if m != nil {
  62. return m.ReqId
  63. }
  64. return ""
  65. }
  66. func (m *Request) GetAIds() []int64 {
  67. if m != nil {
  68. return m.AIds
  69. }
  70. return nil
  71. }
  72. func (m *Request) GetPlatform() string {
  73. if m != nil {
  74. return m.Platform
  75. }
  76. return ""
  77. }
  78. func (m *Request) GetTs() int64 {
  79. if m != nil {
  80. return m.Ts
  81. }
  82. return 0
  83. }
  84. func init() {
  85. proto.RegisterType((*Request)(nil), "jlb.Request")
  86. }
  87. func init() { proto.RegisterFile("test.proto", fileDescriptor_c161fcfdc0c3ff1e) }
  88. var fileDescriptor_c161fcfdc0c3ff1e = []byte{
  89. // 154 bytes of a gzipped FileDescriptorProto
  90. 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2a, 0x49, 0x2d, 0x2e,
  91. 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xce, 0xca, 0x49, 0x52, 0x4a, 0xe4, 0x62, 0x0f,
  92. 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x11, 0x12, 0xe5, 0x62, 0x2b, 0x4a, 0x2d, 0x8c, 0xcf, 0x4c,
  93. 0x91, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x62, 0x2d, 0x4a, 0x2d, 0xf4, 0x4c, 0x11, 0x12, 0xe6,
  94. 0x62, 0x4d, 0x8c, 0xcf, 0x4c, 0x29, 0x96, 0x60, 0x52, 0x60, 0xd6, 0x60, 0x0e, 0x62, 0x49, 0xf4,
  95. 0x4c, 0x29, 0x16, 0x92, 0xe2, 0xe2, 0x28, 0xc8, 0x49, 0x2c, 0x49, 0xcb, 0x2f, 0xca, 0x95, 0x60,
  96. 0x06, 0xab, 0x86, 0xf3, 0x85, 0xf8, 0xb8, 0x98, 0x4a, 0x8a, 0x25, 0x58, 0x14, 0x18, 0x35, 0x98,
  97. 0x83, 0x98, 0x4a, 0x8a, 0x9d, 0x04, 0x4e, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1,
  98. 0x23, 0x39, 0xc6, 0x19, 0x8f, 0xe5, 0x18, 0x92, 0xd8, 0xc0, 0x0e, 0x30, 0x06, 0x04, 0x00, 0x00,
  99. 0xff, 0xff, 0x73, 0x6a, 0x49, 0x0d, 0x8e, 0x00, 0x00, 0x00,
  100. }
  101. func (m *Request) Marshal() (dAtA []byte, err error) {
  102. size := m.Size()
  103. dAtA = make([]byte, size)
  104. n, err := m.MarshalToSizedBuffer(dAtA[:size])
  105. if err != nil {
  106. return nil, err
  107. }
  108. return dAtA[:n], nil
  109. }
  110. func (m *Request) MarshalTo(dAtA []byte) (int, error) {
  111. size := m.Size()
  112. return m.MarshalToSizedBuffer(dAtA[:size])
  113. }
  114. func (m *Request) MarshalToSizedBuffer(dAtA []byte) (int, error) {
  115. i := len(dAtA)
  116. _ = i
  117. var l int
  118. _ = l
  119. if m.XXX_unrecognized != nil {
  120. i -= len(m.XXX_unrecognized)
  121. copy(dAtA[i:], m.XXX_unrecognized)
  122. }
  123. if m.Ts != 0 {
  124. i = encodeVarintTest(dAtA, i, uint64(m.Ts))
  125. i--
  126. dAtA[i] = 0x20
  127. }
  128. if len(m.Platform) > 0 {
  129. i -= len(m.Platform)
  130. copy(dAtA[i:], m.Platform)
  131. i = encodeVarintTest(dAtA, i, uint64(len(m.Platform)))
  132. i--
  133. dAtA[i] = 0x1a
  134. }
  135. if len(m.AIds) > 0 {
  136. dAtA2 := make([]byte, len(m.AIds)*10)
  137. var j1 int
  138. for _, num1 := range m.AIds {
  139. num := uint64(num1)
  140. for num >= 1<<7 {
  141. dAtA2[j1] = uint8(uint64(num)&0x7f | 0x80)
  142. num >>= 7
  143. j1++
  144. }
  145. dAtA2[j1] = uint8(num)
  146. j1++
  147. }
  148. i -= j1
  149. copy(dAtA[i:], dAtA2[:j1])
  150. i = encodeVarintTest(dAtA, i, uint64(j1))
  151. i--
  152. dAtA[i] = 0x12
  153. }
  154. if len(m.ReqId) > 0 {
  155. i -= len(m.ReqId)
  156. copy(dAtA[i:], m.ReqId)
  157. i = encodeVarintTest(dAtA, i, uint64(len(m.ReqId)))
  158. i--
  159. dAtA[i] = 0xa
  160. }
  161. return len(dAtA) - i, nil
  162. }
  163. func encodeVarintTest(dAtA []byte, offset int, v uint64) int {
  164. offset -= sovTest(v)
  165. base := offset
  166. for v >= 1<<7 {
  167. dAtA[offset] = uint8(v&0x7f | 0x80)
  168. v >>= 7
  169. offset++
  170. }
  171. dAtA[offset] = uint8(v)
  172. return base
  173. }
  174. func (m *Request) Size() (n int) {
  175. if m == nil {
  176. return 0
  177. }
  178. var l int
  179. _ = l
  180. l = len(m.ReqId)
  181. if l > 0 {
  182. n += 1 + l + sovTest(uint64(l))
  183. }
  184. if len(m.AIds) > 0 {
  185. l = 0
  186. for _, e := range m.AIds {
  187. l += sovTest(uint64(e))
  188. }
  189. n += 1 + sovTest(uint64(l)) + l
  190. }
  191. l = len(m.Platform)
  192. if l > 0 {
  193. n += 1 + l + sovTest(uint64(l))
  194. }
  195. if m.Ts != 0 {
  196. n += 1 + sovTest(uint64(m.Ts))
  197. }
  198. if m.XXX_unrecognized != nil {
  199. n += len(m.XXX_unrecognized)
  200. }
  201. return n
  202. }
  203. func sovTest(x uint64) (n int) {
  204. return (math_bits.Len64(x|1) + 6) / 7
  205. }
  206. func sozTest(x uint64) (n int) {
  207. return sovTest(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  208. }
  209. func (m *Request) Unmarshal(dAtA []byte) error {
  210. l := len(dAtA)
  211. iNdEx := 0
  212. for iNdEx < l {
  213. preIndex := iNdEx
  214. var wire uint64
  215. for shift := uint(0); ; shift += 7 {
  216. if shift >= 64 {
  217. return ErrIntOverflowTest
  218. }
  219. if iNdEx >= l {
  220. return io.ErrUnexpectedEOF
  221. }
  222. b := dAtA[iNdEx]
  223. iNdEx++
  224. wire |= uint64(b&0x7F) << shift
  225. if b < 0x80 {
  226. break
  227. }
  228. }
  229. fieldNum := int32(wire >> 3)
  230. wireType := int(wire & 0x7)
  231. if wireType == 4 {
  232. return fmt.Errorf("proto: Request: wiretype end group for non-group")
  233. }
  234. if fieldNum <= 0 {
  235. return fmt.Errorf("proto: Request: illegal tag %d (wire type %d)", fieldNum, wire)
  236. }
  237. switch fieldNum {
  238. case 1:
  239. if wireType != 2 {
  240. return fmt.Errorf("proto: wrong wireType = %d for field ReqId", wireType)
  241. }
  242. var stringLen uint64
  243. for shift := uint(0); ; shift += 7 {
  244. if shift >= 64 {
  245. return ErrIntOverflowTest
  246. }
  247. if iNdEx >= l {
  248. return io.ErrUnexpectedEOF
  249. }
  250. b := dAtA[iNdEx]
  251. iNdEx++
  252. stringLen |= uint64(b&0x7F) << shift
  253. if b < 0x80 {
  254. break
  255. }
  256. }
  257. intStringLen := int(stringLen)
  258. if intStringLen < 0 {
  259. return ErrInvalidLengthTest
  260. }
  261. postIndex := iNdEx + intStringLen
  262. if postIndex < 0 {
  263. return ErrInvalidLengthTest
  264. }
  265. if postIndex > l {
  266. return io.ErrUnexpectedEOF
  267. }
  268. m.ReqId = string(dAtA[iNdEx:postIndex])
  269. iNdEx = postIndex
  270. case 2:
  271. if wireType == 0 {
  272. var v int64
  273. for shift := uint(0); ; shift += 7 {
  274. if shift >= 64 {
  275. return ErrIntOverflowTest
  276. }
  277. if iNdEx >= l {
  278. return io.ErrUnexpectedEOF
  279. }
  280. b := dAtA[iNdEx]
  281. iNdEx++
  282. v |= int64(b&0x7F) << shift
  283. if b < 0x80 {
  284. break
  285. }
  286. }
  287. m.AIds = append(m.AIds, v)
  288. } else if wireType == 2 {
  289. var packedLen int
  290. for shift := uint(0); ; shift += 7 {
  291. if shift >= 64 {
  292. return ErrIntOverflowTest
  293. }
  294. if iNdEx >= l {
  295. return io.ErrUnexpectedEOF
  296. }
  297. b := dAtA[iNdEx]
  298. iNdEx++
  299. packedLen |= int(b&0x7F) << shift
  300. if b < 0x80 {
  301. break
  302. }
  303. }
  304. if packedLen < 0 {
  305. return ErrInvalidLengthTest
  306. }
  307. postIndex := iNdEx + packedLen
  308. if postIndex < 0 {
  309. return ErrInvalidLengthTest
  310. }
  311. if postIndex > l {
  312. return io.ErrUnexpectedEOF
  313. }
  314. var elementCount int
  315. var count int
  316. for _, integer := range dAtA[iNdEx:postIndex] {
  317. if integer < 128 {
  318. count++
  319. }
  320. }
  321. elementCount = count
  322. if elementCount != 0 && len(m.AIds) == 0 {
  323. m.AIds = make([]int64, 0, elementCount)
  324. }
  325. for iNdEx < postIndex {
  326. var v int64
  327. for shift := uint(0); ; shift += 7 {
  328. if shift >= 64 {
  329. return ErrIntOverflowTest
  330. }
  331. if iNdEx >= l {
  332. return io.ErrUnexpectedEOF
  333. }
  334. b := dAtA[iNdEx]
  335. iNdEx++
  336. v |= int64(b&0x7F) << shift
  337. if b < 0x80 {
  338. break
  339. }
  340. }
  341. m.AIds = append(m.AIds, v)
  342. }
  343. } else {
  344. return fmt.Errorf("proto: wrong wireType = %d for field AIds", wireType)
  345. }
  346. case 3:
  347. if wireType != 2 {
  348. return fmt.Errorf("proto: wrong wireType = %d for field Platform", wireType)
  349. }
  350. var stringLen uint64
  351. for shift := uint(0); ; shift += 7 {
  352. if shift >= 64 {
  353. return ErrIntOverflowTest
  354. }
  355. if iNdEx >= l {
  356. return io.ErrUnexpectedEOF
  357. }
  358. b := dAtA[iNdEx]
  359. iNdEx++
  360. stringLen |= uint64(b&0x7F) << shift
  361. if b < 0x80 {
  362. break
  363. }
  364. }
  365. intStringLen := int(stringLen)
  366. if intStringLen < 0 {
  367. return ErrInvalidLengthTest
  368. }
  369. postIndex := iNdEx + intStringLen
  370. if postIndex < 0 {
  371. return ErrInvalidLengthTest
  372. }
  373. if postIndex > l {
  374. return io.ErrUnexpectedEOF
  375. }
  376. m.Platform = string(dAtA[iNdEx:postIndex])
  377. iNdEx = postIndex
  378. case 4:
  379. if wireType != 0 {
  380. return fmt.Errorf("proto: wrong wireType = %d for field Ts", wireType)
  381. }
  382. m.Ts = 0
  383. for shift := uint(0); ; shift += 7 {
  384. if shift >= 64 {
  385. return ErrIntOverflowTest
  386. }
  387. if iNdEx >= l {
  388. return io.ErrUnexpectedEOF
  389. }
  390. b := dAtA[iNdEx]
  391. iNdEx++
  392. m.Ts |= int64(b&0x7F) << shift
  393. if b < 0x80 {
  394. break
  395. }
  396. }
  397. default:
  398. iNdEx = preIndex
  399. skippy, err := skipTest(dAtA[iNdEx:])
  400. if err != nil {
  401. return err
  402. }
  403. if (skippy < 0) || (iNdEx+skippy) < 0 {
  404. return ErrInvalidLengthTest
  405. }
  406. if (iNdEx + skippy) > l {
  407. return io.ErrUnexpectedEOF
  408. }
  409. m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...)
  410. iNdEx += skippy
  411. }
  412. }
  413. if iNdEx > l {
  414. return io.ErrUnexpectedEOF
  415. }
  416. return nil
  417. }
  418. func skipTest(dAtA []byte) (n int, err error) {
  419. l := len(dAtA)
  420. iNdEx := 0
  421. depth := 0
  422. for iNdEx < l {
  423. var wire uint64
  424. for shift := uint(0); ; shift += 7 {
  425. if shift >= 64 {
  426. return 0, ErrIntOverflowTest
  427. }
  428. if iNdEx >= l {
  429. return 0, io.ErrUnexpectedEOF
  430. }
  431. b := dAtA[iNdEx]
  432. iNdEx++
  433. wire |= (uint64(b) & 0x7F) << shift
  434. if b < 0x80 {
  435. break
  436. }
  437. }
  438. wireType := int(wire & 0x7)
  439. switch wireType {
  440. case 0:
  441. for shift := uint(0); ; shift += 7 {
  442. if shift >= 64 {
  443. return 0, ErrIntOverflowTest
  444. }
  445. if iNdEx >= l {
  446. return 0, io.ErrUnexpectedEOF
  447. }
  448. iNdEx++
  449. if dAtA[iNdEx-1] < 0x80 {
  450. break
  451. }
  452. }
  453. case 1:
  454. iNdEx += 8
  455. case 2:
  456. var length int
  457. for shift := uint(0); ; shift += 7 {
  458. if shift >= 64 {
  459. return 0, ErrIntOverflowTest
  460. }
  461. if iNdEx >= l {
  462. return 0, io.ErrUnexpectedEOF
  463. }
  464. b := dAtA[iNdEx]
  465. iNdEx++
  466. length |= (int(b) & 0x7F) << shift
  467. if b < 0x80 {
  468. break
  469. }
  470. }
  471. if length < 0 {
  472. return 0, ErrInvalidLengthTest
  473. }
  474. iNdEx += length
  475. case 3:
  476. depth++
  477. case 4:
  478. if depth == 0 {
  479. return 0, ErrUnexpectedEndOfGroupTest
  480. }
  481. depth--
  482. case 5:
  483. iNdEx += 4
  484. default:
  485. return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
  486. }
  487. if iNdEx < 0 {
  488. return 0, ErrInvalidLengthTest
  489. }
  490. if depth == 0 {
  491. return iNdEx, nil
  492. }
  493. }
  494. return 0, io.ErrUnexpectedEOF
  495. }
  496. var (
  497. ErrInvalidLengthTest = fmt.Errorf("proto: negative length found during unmarshaling")
  498. ErrIntOverflowTest = fmt.Errorf("proto: integer overflow")
  499. ErrUnexpectedEndOfGroupTest = fmt.Errorf("proto: unexpected end of group")
  500. )

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

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xjdG1laQ_size_16_color_FFFFFF_t_70 2

发表评论

表情:
评论列表 (有 0 条评论,284人围观)

还没有评论,来说两句吧...

相关阅读

    相关 golang使用protobuf简易教程

    golang使用protobuf简易教程 google公司发布的一套开源编码规则,基于二进制流的序列化传输,可以转换成多种编程语言,几乎涵盖了市面上所有的主流编程语言,当