-3

In C#, there's a method CreateInstance of class Activator which allows you to create an instance of the reflected type as it is shown here.

Is there a way to do the same tihing in Go programming language? I would like to take a type from a plugin (.so on Linux or .dll on Windows) by the name of the type and create an instance of this type in my code. How can I do that?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
zergon321
  • 210
  • 1
  • 10
  • Possible duplicate [How to get Type representation from name via reflection?](https://stackoverflow.com/questions/40879748/how-to-get-type-representation-from-name-via-reflection/40882963#40882963) – icza May 31 '21 at 18:55
  • @icza No, it isn't. The question you referred to doesn't solve my problem. I'm talking about plugins so my type exists at the execution time. – zergon321 May 31 '21 at 20:29

1 Answers1

2

Is there a way to do the same tihing in Go programming language?

No.

I would like to take a type from a plugin (.so on Linux or .dll on Windows) by the name of the type and create an instance of this type in my code. How can I do that?

You cannot. Sorry. You must redesign. Go is not C# and has different rules. One rule is that you can't create instances of types by their name alone. But if your plugin announces which types it contains and their names via some kind of registry (think of package image) then your main code can look up names and types in that registry and reflect normally.

icza
  • 389,944
  • 63
  • 907
  • 827
Volker
  • 40,468
  • 7
  • 81
  • 87