从名称获取reflect.Type
如果我有一个类型的名称(即“容器/向量”),有没有办法查找具有给定名称的reflect.Type?我正在尝试编写一个简单的数据库支持的工作队列系统,如果没有此功能,这将非常困难。
If I have a name of a type (i.e "container/vector"), is there a way to lookup the reflect.Type that has the given name? I'm trying to write a simple database-backed workqueue system and this it would be very difficult without this feature.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不出这是如何以任何微不足道的方式(或根本)实现的,因为名称解析是编译器/链接器的一部分,而不是运行时的一部分。
但是, http://github.com/nsf/gocode 可能会提供一些想法。虽然我很确定可以通过处理 $GOROOT 中的 .a 文件来实现,所以我仍然不知道如何获得reflect.Type。也许 exp/eval 包更成熟?
当然,如果您知道您将遇到的所有可能的类型,您始终可以制作一个reflect.Type 的映射。但我假设您正在处理不可预测的输入,否则您会想到这一点。
I can't see how this would be possible in any trivial way (or at all), since name resolution is part of the compiler/linker, not the runtime.
However, http://github.com/nsf/gocode might offer up some ideas. Though I'm pretty sure that works by processing the .a files in $GOROOT, so I still don't see how you'd get the reflect.Type. Maybe if the exp/eval package was more mature?
Of course if you know all the possible types you'll encounter, you could always make a map of the reflect.Type. But I'm assuming you're working with unpredictable input, or you would've thought of that.
创建
reflect.Type
的唯一方法是首先拥有预期类型的具体值。您甚至无法从基本类型 (T
) 创建复合类型,例如切片 ([]T
)。从
string
转换为reflect.Type
的唯一方法是您自己输入映射。Only way to create a
reflect.Type
is by having a concrete value of the intended type first. You can't even create composite-types, such as a slice ([]T
), from a base type (T
).The only way to go from a
string
to areflect.Type
is by entering the mapping yourself.