This is a creation in Article, where the information may have evolved or changed.
Go 1.8 provides us with a new tool for creating a shared library called plugins! Let's create and use a plugin. The current plugin only works on Linux and Darwin.
Install 1.8BETA1, do not make instructions.
Create a plug-in method to Aplugin.go:
package mainfunc Add(x, y int) int { return x+y}func Subtract(x, y int) int { return x-y}
Then build the plugin:
Run the following command to build the plugin:
go build -buildmode=plugin
Build the specified file plugin aplugin.go to aplugin.so:
go build -buildmode=plugin -o aplugin.so aplugin.go
Loading plugins:
p, _ := plugin.Open("./aplugin.so")//p, err := plugin.Open("./aplugin.so")
Call Plugin:
add, _ := p.Lookup("Add")sub, _ := p.Lookup("Subtract")
Using plugins:
sum := add.(func(int, int) int)(11, 2)fmt.Println(sum)subt := sub.(func(int, int) int)(11, 2)fmt.Println(subt)