My proto file look like this
message DeviceOption {
string ApID = 1;
string Other = 2;
}
After run protoc, the DeviceOption
struct generated like this:
type DeviceOption struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ApID string `protobuf:"bytes,1,opt,name=ApID,proto3" json:"ApID,omitempty"`
Other string `protobuf:"bytes,2,opt,name=Other,proto3" json:"Other,omitempty"`
}
Now, in server I wanna parse all available fields using https://pkg.go.dev/reflect. My code is like:
v := reflect.ValueOf(pb.DeviceOption{
ApID: "random1",
Other: "random2",
})
for i := 0; i < v.NumField(); i++ {
// Do my thing
}
v.NumField()
returns 5, which means it includes all the fields, including state, sizeCache, unknownFields which we don't want. We just want ApID and Other.
Is there any other way to let reflect return only data (exported) fields, and ignore metadata (unexported) fields?
Aucun commentaire:
Enregistrer un commentaire