Developer guide for using protobuf in Go

What is protobuf?

官方给出的定义是:Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.

Translation: Google’s language-neutral, platform-neutral, and extensible mechanism is used to serialize structured data, such as XML, but is smaller, faster, and simpler. You only need to define the structure of the data once, and then you can use the specially generated source code to easily write and read structured data between various data streams and various languages.

advantage:

  • Support multiple languages. (Go, C++, Java and other languages)
  • Simple and efficient

For details, please refer to: protobuf official website

1. Install protobuf

github download address: https://github.com/golang/protobuf/releases

2. Install protoc-gen-go

Very simple, just run the following command in the go module

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

3.1. Define protobuf

Need to create .protoa file with the suffix .

syntax = "proto3";
package test;
option go_package = "./;test";
message Request {
  string query = 1;
  int32 page = 2;
  int32 size = 3;
}
  • proto3: is the version number, if not defined, the proto2 version will be used by default, and it syntax = "proto3";must be placed in the first line
  • message: Message definition. Each field has a name, type and number. Note: Each field has a unique number. The minimum field number specified is 1, and the maximum field number is 2^29-1 or 536870911. The numbers 19000 to 19999 cannot be used.
  • package: The package where the declaration is located
  • go_package: The package to which the declared file belongs in the complete import path of the Go package

3.2. Message type

.proto type Types of Go
double float64
float float32
int32 int32
int64 int64
uint32 uint32
uint64 uint64
sint32 int32
sint64 int64
fixed32 uint32
fixed64 uint64
sfixed32 int32
sfixed64 int64
bool bool
string string
bytes []byte

3.3. Enumeration type

Must have a zero value as its first element;

enum Week {
    Sunday=0;
    Monday=1;
    Tuesday=2;
}

3.4. Slice type

Just add repeatedit before the element type of the message type , and it will be converted to the go slice type by default when compiling.

message Request {
  repeated string query = 1;  // 切片类型
  int32 page = 2;
  int32 size = 3;
}

3.5. Nested types

在消息类型内,是可以定义一个消息类型。 `condition`消息属于了`SearchRequest`的内部消息:
message Request {
    message Condition {
        string username = 1;
        string password = 2;
    }
    int32 page = 2;
    int32 size = 3;
}

Of course, you can also use the message type defined outside the message type, so that the message type can be reused.

message Condition {
    string username = 1;
    string password = 2;
}
message Request {
    Condition condition = 1; // 引入消息类型
    int32 page = 2;
    int32 size = 3;
}

3.6.map type

To define the map type, protobuf also provides a very convenient way.


map<key_type, value_type> map_field = N; // map的定义

message Request {
  map<string, string> query = 1; // 定义一个字符串键和值的map类型
}

have to be aware of is:

  • map type cannot be usedrepeated
  • If you provide a key but no value for a mapped field, the behavior when serializing the field depends on the language. In C++, Java, and Python, the default value of a type is serialized, while in other languages, there is no serialized value.

4. Service definition

If you use it RPCas a microservice development, then you can .protodefine the RPC service interface in the file:

service UserService {
  rpc Search(Request) returns (Response);
}
  • UserService: The name of the service interface you defined;
  • Search: The name of the service interface method you defined;
  • Request: Service method parameter information;
  • Response: Service response result information.

Compile

Switch to .protothe file directory and execute:

protoc --go_out=. *.proto
  • –Go_out: Generate Go codeDST_DIR
  • *.proto: proto file path

Guess you like

Origin blog.csdn.net/Xiao_W_u/article/details/115318154