Proto Grammar

Overview

Protocol buffers is Google language neutral, platform neutral, and extensible structured data serializer – but smaller, faster, and simpler than XML.You define how a data is structured and then you can easily write structured data into data streams using specially generated source codes and read structured data in various languages.

::note
in go-Zero framework development, we use Language Guide (proto3). :::

Getting started

Example 1. Write the simplest rpc service

  1. // Statement proto syntax version, fixed value
  2. syntax = "proto3";
  3. // proto package name
  4. package greet;
  5. // Generate package name after golang code
  6. option go_package = "example/proto/greet";
  7. // Defining request body
  8. message SahelloReq {}
  9. // Defines response body
  10. message SayhelloResp {}
  11. // Defines Greet service
  12. service Greet
  13. // Defines an Sayhelo a rpc method, The body and response body are required.
  14. rpc SayHello(SayHelloReq) returns (SayHelloResp);
  15. }

Example 2. Write a streaming request service sample

  1. // Statement proto syntax version, fixed value
  2. syntax = "proto3";
  3. // proto package name
  4. package greet;
  5. // Generate package name after golang code
  6. option go_package = "example/proto/greet";
  7. // Defines the structural body
  8. message SayhelloReq {}
  9. message SayhelloResp {}
  10. message SendMessageReq6
  11. string message = 1;
  12. }
  13. message SendMessageRespondageRespect
  14. int32 status = 1;
  15. }
  16. message GetMessageReq?
  17. int32 id = 1;
  18. }
  19. message GetMessageRespondageResponse
  20. string message = 1;
  21. }
  22. // Defines the Greet service
  23. the service Greet {
  24. // Defines the client flow rpc
  25. rpc SendMessage(stam SendMessageReq) returns (SendMessageRespond);
  26. // Defines service endpoint rpc
  27. rpc GetMessage(GetMessageReq) returns (stream GetMessageResponse);
  28. // Defining two-way flow rpc
  29. rpc PushMessage(stream Ream SendMessageReq) returns (stream GetMessageResponse);
  30. }

Example 3. Write rpc group examples

rpc groups are distinguished primarily by service name.

  1. // Statement proto syntax version, fixed value
  2. syntax = "proto3";
  3. // proto package name
  4. package greet;
  5. // Generate package name after golang code
  6. option go_package = "example/proto/greet";
  7. // Defines the structural body
  8. message SendMessageReq6
  9. string message = 1;
  10. }
  11. message SendMessageRespect \
  12. int32 status = 1;
  13. }
  14. message GetMessageReq\
  15. int32 id = 1;
  16. } }
  17. message GetMessageRespect L
  18. string message = 1;
  19. }
  20. // Defines the Greet service
  21. service Greet {
  22. // Defines the client flow rpc
  23. rpc SendMessage(stream Req) returns (SendMessageRespond);
  24. // Defines service endpoint rpc
  25. rpc GetMessage(GetMessageReq) returns (stream GetMessageResponse);
  26. // Defines two-way flow rpc
  27. rpc PushMessage(stream Ream SendMessageReq) returns (stream GetMessageResponse);
  28. }
  29. // Definition of the Greet service
  30. service Greet LO
  31. rpc Sayhello(SayhelloReq) returnns (SayHelloRespond);
  32. }
  33. // Defines the Message service
  34. service Messaging {
  35. // Defines the client flow rpc
  36. rpc SendMessage(stream Req) returns (SendMessageRespond);
  37. // Defines server stream rpc
  38. rpc GetMessage(GetMessageReq) returns (stream GetMessageResponse);
  39. // Defines two-way flow rpc
  40. rpc PushMessage(streamream SendMessageReq) returns (stream GetMessageResponse);
  41. }

Example 4. Message Example

  1. // declare proto syntax version, fixed value
  2. syntax = "proto3";
  3. // proto package name
  4. package greet;
  5. // Package name after generating golang code
  6. option go_package = "example/proto/greet";
  7. // define enumeration
  8. enum Status{
  9. UNSPECIFIED = 0;
  10. SUCCESS = 1;
  11. FAILED = 2;
  12. }
  13. // Define the structure
  14. message Base{
  15. int32 code = 1;
  16. string msg = 2;
  17. }
  18. message SendMessageReq{
  19. string message = 1;
  20. }
  21. message SendMessage{
  22. // use enum
  23. Status status = 1;
  24. // array
  25. repeated string array = 2;
  26. // map
  27. map<string,int32> map = 3;
  28. // boolean
  29. bool boolean = 4;
  30. // reserved
  31. reserved 5;
  32. }
  33. message SendMessageResp{
  34. Base base = 1;
  35. SendMessage data = 2;
  36. }
  37. // Define the Greet service
  38. service Greet {
  39. // Define client streaming rpc
  40. rpc SendMessage(stream SendMessageReq) returns (SendMessageResp);
  41. }

Example 5. Proto file introduction

Assume we have the following environment:

  1. Work path:/usr/local/workspace
  2. base.proto path and content:/usr/local/workspace/base/base.proto
  1. syntax = "proto3";
  2. // proto package name
  3. package base;
  4. // Generate package name after golang code
  5. option go_package = "example/proto/base";
  6. message Base
  7. int32 code = 1;
  8. string msg = 2;
  9. }

Now you need a new /usr/local/workspace/greet.proto file, and reference /usr/local/workspace/base/base.proto Structure Base We look at the simple quotation example:

  1. // declare proto syntax version, fixed value
  2. syntax = "proto3";
  3. // proto package name
  4. package greet;
  5. // Package name after generating golang code
  6. option go_package = "example/proto/greet";
  7. // define enumeration
  8. enum Status{
  9. UNSPECIFIED = 0;
  10. SUCCESS = 1;
  11. FAILED = 2;
  12. }
  13. // Define the structure
  14. message Base{
  15. int32 code = 1;
  16. string msg = 2;
  17. }
  18. message SendMessageReq{
  19. string message = 1;
  20. }
  21. message SendMessage{
  22. // use enum
  23. Status status = 1;
  24. // array
  25. repeated string array = 2;
  26. // map
  27. map<string,int32> map = 3;
  28. // boolean
  29. bool boolean = 4;
  30. // reserved
  31. reserved 5;
  32. }
  33. message SendMessageResp{
  34. Base base = 1;
  35. SendMessage data = 2;
  36. }
  37. // Define the Greet service
  38. service Greet {
  39. // Define client streaming rpc
  40. rpc SendMessage(stream SendMessageReq) returns (SendMessageResp);
  41. }
Proto Grammar - 图1TTIPS

When goctl generates gRPC code based on proto:

  1. Message in service (Attendee&) must be in main proto, not supported file
  2. The imported message can only be nested in the main proto
  3. When goctl returns gRPC code, it does not generate the Go code of the imported proto file. Requires auto-pre-generate

References