Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic loading in Golang?

Tags:

java

go

I've got a common project and small projects which act such as connectors in the common project.

I want to create a common project such that when a new connector is developed I don’t have to modify code in the common project. Is it possible to dynamically load the structures in Go, only knowing the path (by putting this path in a file in common project and at runtime load that struct) of the struct and its folders?

connector1
  connector1.go
  /util
  /domain

connectorN
  connectorN.go 
  /domain

commonProject
   main.go
   config.ini

Structure config.ini

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

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

I know that this is possible to do this in Java with code like this, but I am trying to do this in Go. Any ideas?

Class.forName(String) 

or

ClassLoader.loadClass(String):
like image 645
Carlos Andrés García Avatar asked Nov 09 '22 14:11

Carlos Andrés García


1 Answers

I can see two ways to achieve what you describe, but keep in mind, as @icza pointed out, that go produces static binaries, so you can't load external libraries dynamically.

You can, however:

  • use cgo to interface with C code, and load external libraries that way.
  • use the net/rpc package to have several binaries communicate with each other, and load those on demand.
like image 70
Frederik Deweerdt Avatar answered Nov 14 '22 23:11

Frederik Deweerdt