Dynamic loading in the Golang?

I have a common project and small projects that act as connectors in a common project.

I want to create a common project so that when developing a new connector, I do not need to change the code in the general project. Is it possible to dynamically load structures in Go, knowing only the path (by placing this path in the file in the general project and while loading this structure) of the structure and its folders?

connector1 connector1.go /util /domain connectorN connectorN.go /domain commonProject main.go config.ini 

Config.ini structure

 Conector name = connector1 path = ..../connector1/connector1.go Conector name = connectorN path = ..../connectorN/connectorN.go 

I know that this can be done in Java with this code, but I'm trying to do it in Go. Any ideas?

 Class.forName(String) 

or

 ClassLoader.loadClass(String): 
+5
source share
2 answers

I can see two ways to achieve what you are describing, but keep in mind, as @icza noted, that it creates static binaries, so you cannot dynamically load external libraries.

However, you can:

  • use cgo to interact with C code and load external libraries, which is the way to go.
  • use the net/rpc package to exchange multiple binaries with each other and download them on demand.
+1
source

In Java, classes are loaded dynamically, on demand, when they are used / mentioned.

Go creates statically linked source binaries without external dependencies, so you cannot load new “types” or “functions” like you can do in Java using Class.forName() (at least not code written in Go )

0
source

All Articles