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. https://developers.google.com/protocol-buffers
The Context.Protobuf(proto.Message) is the method which sends protos to the client. It accepts a proto.Message value.
The methods you need to know when working with Protocol Buffers are the following:
// Protobuf writes a proto message to the client,// which should be able to read and parse protos too. Protobuf(v proto.Message) (int, error)// ReadProtobuf binds the request body// to the proto message.ReadProtobuf(ptr proto.Message) error
// JSON renders a proto.Message compatible value as JSON.JSON(v interface{}, options ...JSON) (int, error)// ReadJSONProtobuf reads a JSON body request// into the given "ptr" proto.Message.ReadJSONProtobuf(ptr proto.Message, opts ...protojson.UnmarshalOptions) error
typeJSONstruct {// [...other fields] Proto ProtoMarshalOptions}typeProtoMarshalOptionsstruct {// Multiline specifies whether the marshaler// should format the output in// indented-form with every textual element// on a new line.// If Indent is an empty string,// then an arbitrary indent is chosen. Multiline bool// Indent specifies the set of indentation// characters to use in a multiline// formatted output such that every entry// is preceded by Indent and// terminated by a newline. If non-empty,// then Multiline is treated as true.// Indent can only be composed of space or tab characters. Indent string// AllowPartial allows messages that have// missing required fields to marshal// without returning an error.// If AllowPartial is false (the default),// Marshal will return error if there are// any missing required fields. AllowPartial bool// UseProtoNames uses proto field name// instead of lowerCamelCase name in JSON// field names. UseProtoNames bool// UseEnumNumbers emits enum values as numbers. UseEnumNumbers bool// EmitUnpopulated specifies whether to emit unpopulated fields.// It does not emit unpopulated oneof fields// or unpopulated extension fields.// The JSON value emitted for unpopulated fields are as follows:// ╔═══════╤════════════════════════════// ║ JSON │ Protobuf field // ╠═══════╪════════════════════════════// ║ false │ proto3 boolean fields // ║ 0 │ proto3 numeric fields // ║ "" │ proto3 string/bytes fields // ║ null │ proto2 scalar fields // ║ null │ message fields // ║ [] │ list fields // ║ {} │ map fields // ╚═══════╧════════════════════════════ EmitUnpopulated bool}
Example
Let's create a simple application that showcases the use case of all of the above methods.