英文:
How to correctly determine the type of variable google.protobuf.Any?
问题
请告诉我这段代码中有什么问题,为什么变量的类型无法确定,传递的url类型是google.protobuf.Any?
package main
import (
"fmt"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
func main() {
var res protoregistry.MessageTypeResolver = protoregistry.GlobalTypes
typeUrl := "type.googleapis.com/google.protobuf.StringValue"
fmt.Println(protoreflect.FullName(typeUrl))
msgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName("google.protobuf.StringValue"))
if err != nil {
fmt.Println(err)
}
fmt.Println(msgType)
msgType, err = res.FindMessageByURL(typeUrl)
if err != nil {
fmt.Println(err)
}
fmt.Println(msgType)
}
proto: not found
proto: not found
英文:
Please tell me what is wrong in this code, why the type is not determined for the variable, for the transmitted url type google.protobuf.Any?
package main
import (
"fmt"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
func main() {
var res protoregistry.MessageTypeResolver = protoregistry.GlobalTypes
typeUrl := "type.googleapis.com/google.protobuf.StringValue"
fmt.Println(protoreflect.FullName(typeUrl))
msgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName("google.protobuf.StringValue"))
if err != nil {
fmt.Println(err)
}
fmt.Println(msgType)
msgType, err = res.FindMessageByURL(typeUrl)
if err != nil {
fmt.Println(err)
}
fmt.Println(msgType)
}
proto: not found
<nil>
proto: not found
<nil>
答案1
得分: 1
错误是protoregistry.NotFound
,因为在protoregistry.GlobalTypes
中没有注册消息类型。
要注册google.protobuf.Any
,您应该导入包google.golang.org/protobuf/types/known/anypb
。并且google.protobuf.Any
是由该包的init
函数注册的(请参阅源代码)。
要注册google.protobuf.StringValue
,您应该导入包google.golang.org/protobuf/types/known/wrapperspb
。
这是更新后的演示代码,用于查找google.protobuf.StringValue
的消息字节:
package main
import (
"fmt"
"google.golang.org/protobuf/reflect/protoregistry"
// 仅导入该包以产生副作用(初始化)。
// 请参阅https://go.dev/ref/spec#Import_declarations。
_ "google.golang.org/protobuf/types/known/anypb"
_ "google.golang.org/protobuf/types/known/wrapperspb"
)
func main() {
res := protoregistry.GlobalTypes
fmt.Println("NumMessages:", res.NumMessages())
msgType, err := protoregistry.GlobalTypes.FindMessageByName("google.protobuf.StringValue")
if err != nil {
fmt.Println(err)
}
fmt.Println(msgType)
msgType, err = res.FindMessageByURL("type.googleapis.com/google.protobuf.StringValue")
if err != nil {
fmt.Println(err)
}
fmt.Println(msgType)
}
英文:
The error is protoregistry.NotFound
because there are not message types got registered in protoregistry.GlobalTypes
.
To register google.protobuf.Any
, you should import the package google.golang.org/protobuf/types/known/anypb
. And google.protobuf.Any
is registered by the init
function of that package (see the source code).
To register google.protobuf.StringValue
, you should import the package google.golang.org/protobuf/types/known/wrapperspb
.
Here is the updated demo that finds the message byte of google.protobuf.StringValue
:
package main
import (
"fmt"
"google.golang.org/protobuf/reflect/protoregistry"
// importing the package solely for its side-effects (initialization).
// See https://go.dev/ref/spec#Import_declarations.
_ "google.golang.org/protobuf/types/known/anypb"
_ "google.golang.org/protobuf/types/known/wrapperspb"
)
func main() {
res := protoregistry.GlobalTypes
fmt.Println("NumMessages:", res.NumMessages())
msgType, err := protoregistry.GlobalTypes.FindMessageByName("google.protobuf.StringValue")
if err != nil {
fmt.Println(err)
}
fmt.Println(msgType)
msgType, err = res.FindMessageByURL("type.googleapis.com/google.protobuf.StringValue")
if err != nil {
fmt.Println(err)
}
fmt.Println(msgType)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论