Following this tutorial and the github repo I understood the use of plugins.
The tutorial compiles each file separately into so files.
go build -buildmode=plugin -o eng/eng.so eng/greeter.go
go build -buildmode=plugin -o chi/chi.so chi/greeter.go
How can I merge two files into a single .so file? I tried following command by separating files through space
go build -buildmode=plugin -o bin/langs.so src/test/eng/greeter.go src/test/chi/greeter.go
The error:
named files must all be in one directory; have src/test/eng/ and src/test/chi/
The idea is to have a single .so files from different packages.
Edit: I guess the follow up question would be how to combine all .so
files into one archive if one has several packages of a library and go only allows one .so
file per package.
You can't put them in different folders because they should have same package name (main). But you can put them in different files like this :
file1:
package main
import "fmt"
type greeting_en string
func (g greeting_en) Greet() {
fmt.Println("Hello Universe")
}
var GreeterEn greeting_en
file2:
package main
import "fmt"
type greeting_chi string
func (g greeting_chi) Greet() {
fmt.Println("你好宇宙")
}
var GreeterChi greeting_chi
compile them like this :
go build -buildmode=plugin -o ./langs.go
And load langs like this :
mod = "./langs.so"
plug, _ := plugin.Open(mod)
EnglishGreeter,_ := plug.Lookup("GreeterEn")
ChineseGreeter,_ := plug.Lookup("GreeterChi")