在运行时通过反射按名称动态实例化结构

Go 中是否有任何与 Java 等语言提供的动态类实例化功能等效的东西(注意:为了简洁起见,此处省略了必要的异常处理逻辑):


Class cls = Class.forName("org.company.domain.User");

Constructor<User> userConstructor = cls.getConstructor();

User user1 = userConstructor.newInstance();

上面的简短 Java 代码段实质上是通过提供的完全限定的类路径字符串获取对类的引用,然后使用类引用来获取对零参数构造函数(如果存在)的引用,最后使用构造函数来获取对类实例的引用。


我还没有在Go中找到一个类似机制的例子,可以达到类似的结果。更具体地说,go 中的反射包似乎要求调用方已经引用了他们希望实例化的结构类型。这方面的标准成语似乎如下:


reflect.New(reflect.TypeOf(domain.User))

注:提供给反射的参数。TypeOf 函数必须是 Type 而不是字符串。是否可以在 Go 中通过反射包实例化结构,只使用其完全限定的名称?


MM们
浏览 109回答 1
1回答

海绵宝宝撒

Kubernetes 在运行时处理这个确切的过程。方案结构。我们的想法是,您使用名称或其他标识符注册类型,然后您可以根据标识符随意请求这些类型的新实例。一般来说,此标识符是在序列化过程中派生的,而不是硬编码到源中。正如您所说,问题在于,您最初需要创建一个新实例。虽然这种模式并不常见,但我在我的职业生涯中遇到过两种情况,这是合乎逻辑的解决方案。以下是K8s为实现这一目标所做的非常精简版本的示例,它可能适用于您尝试做的事情,在这里它正在发挥作用:runtime.Schemepackage mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "reflect")type Scheme struct {&nbsp; &nbsp; types map[string]reflect.Type}func (s *Scheme) RegisterType(name string, t interface{}) {&nbsp; &nbsp; a := reflect.TypeOf(t)&nbsp; &nbsp; s.types[name] = a}func (s *Scheme) New(name string) (interface{}, error) {&nbsp; &nbsp; t, ok := s.types[name]&nbsp; &nbsp; if !ok {&nbsp; &nbsp; &nbsp; &nbsp; return nil, fmt.Errorf("unrecognized type name: %s", name)&nbsp; &nbsp; }&nbsp; &nbsp; return reflect.New(t).Interface(), nil}func NewScheme() *Scheme {&nbsp; &nbsp; return &Scheme{types: map[string]reflect.Type{}}}type MyType struct {&nbsp; &nbsp; Foo string}func main() {&nbsp; &nbsp; scheme := NewScheme()&nbsp; &nbsp; scheme.RegisterType("my.type", MyType{})&nbsp; &nbsp; myType, _ := scheme.New("my.type")&nbsp; &nbsp; myType.(*MyType).Foo = "bar"&nbsp; &nbsp; fmt.Println("%+v", myType)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go