XXX_* 输入生成的 *.pb.go 文件

2024-02-11

我正在研究一个tutorial https://ewanvalentine.io/microservices-in-golang-part-1/关于 gRPC。当我生成.pb.go文件,我得到一些XXX_*输入我的结构。

这是我的consignment.proto file:

syntax = "proto3";

package go.micro.srv.consignment; 

service ShippingService {
    rpc CreateConsignment(Consignment) returns (Response) {}
}

message Consignment {
    string id = 1;
    string description = 2;
    int32 weight = 3;
    repeated Container containers = 4;
    string vessel_id = 5;
}

message Container {
    string id = 1;
    string customer_id = 2;
    string origin = 3;
    string user_id = 4;
}

message Response {
    bool created = 1;
    Consignment consignment = 2;
}

这是结构体中的.pb.go文件。谁能告诉我为什么有3个XXX类型在我的struct?该结构不应该反映我在我的定义中定义的内容吗?proto?

type Consignment struct {
    Id                   string       `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
    Description          string       `protobuf:"bytes,2,opt,name=description" json:"description,omitempty"`
    Weight               int32        `protobuf:"varint,3,opt,name=weight" json:"weight,omitempty"`
    Containers           []*Container `protobuf:"bytes,4,rep,name=containers" json:"containers,omitempty"`
    VesselId             string       `protobuf:"bytes,5,opt,name=vessel_id,json=vesselId" json:"vessel_id,omitempty"`
    XXX_NoUnkeyedLiteral struct{}     `json:"-"`
    XXX_unrecognized     []byte       `json:"-"`
    XXX_sizecache        int32        `json:"-"`
}

The XXX_Protobuf 库使用类型来存储未知字段。当您解码原型时,序列化数据中可能存在库不知道如何处理的其他字段。例如,当数据的读取器和写入器使用原始文件的不同副本时,就会发生这种情况。此功能有助于实现不同时间构建的客户端和服务之间的向后兼容性。

此外,XXX 字段允许您公开扩展 https://developers.google.com/protocol-buffers/docs/proto#extensions,这是 Proto2 的一部分。它们在 Proto3 中被删除,以支持Any https://developers.google.com/protocol-buffers/docs/proto3#any,但图书馆仍然需要支持他们。

至于你应该用这些做什么?我只会让它们单独存在,并且不会引用它们。您不需要设置它们,也不需要读取它们。 Go protobuf 库将为您处理它们。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XXX_* 输入生成的 *.pb.go 文件 的相关文章

随机推荐