在Go中,我如何测试一个结构的元素是否属于Any (protobuf):*any.Any类型?我想要遍历结构的每个元素,并根据元素的类型进行切换。以下是消息的字段描述符:
FieldDescriptor{Syntax: proto3, FullName:, Number: 3, Cardinality: optional, Kind: message, HasJSONName: true, JSONName:, HasPresence: true, Oneof:, Message: google.protobuf.Any}下面是我的代码:
func doSomething(src protoreflect.Message) {
src.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
switch {
case fd.IsList():
// do something
case fd.IsMap():
// do something
case fd.Message() != nil:
// do something
case fd.Kind() == protoreflect.BytesKind:
// do something
case *test if message is Any* :
// do something
default:
// do something
}
return true
})
}我想要一种比ex更正确的方式:
if fd.Message() != nil{
fd.Message().FullName() == "google.protobuf.Any"
}发布于 2020-12-29 11:38:29
你可以有一个开关盒来检查类型。
switch v := fd.(type) {
default:
fmt.Printf("unexpected type %T", v)
case string:
//do something
} https://stackoverflow.com/questions/65484519
复制相似问题