Is there a way that you can build a binary file (shared or static library) in go language with bunch of source files and distribute it along with it's interfaces.
It's not to be distributed as an executable but a linkable static or shared library with interfaces at compile or run time. (Just like in C you distribute .a
or .so
file along with header files)
Unlike open source github based libraries out there it's a closed source project that i'm trying to port for Go.
Yes it's possible . You can build your code as a static library and link it in runtime with this command :
go build -buildmode=plugin
You can use built plugin like this :
A Symbol is a pointer to a variable or function.
For example, a plugin defined as
package main
import "fmt"
var V int
func F() { fmt.Printf("Hello, number %d
", V) }
may be loaded with the Open function and then the exported package symbols V and F can be accessed
p, err := plugin.Open("plugin_name.so")
if err != nil {
panic(err)
}
v, err := p.Lookup("V")
if err != nil {
panic(err)
}
f, err := p.Lookup("F")
if err != nil {
panic(err)
}
*v.(*int) = 7
f.(func())() // prints "Hello, number 7"
type Symbol interface{}
Check out built in support for Plugin in go for more info