golang plugin framework for hot update, go version >= 1.8
- get hotplugin
go get github.com/hepeichun/hotplugin
- write a plugin with Load, Unload and other functions like this
//testplugin.go
package main
import (
"fmt"
"log"
)
const (
pluginName = "testplugin"
pluginVersion = 0x00010000
)
func Load(register func(name string, version uint64) error) error {
err := register(pluginName, pluginVersion)
if err != nil {
log.Println(err.Error())
return err
}
log.Println("loading test plugin")
return nil
}
func Unload() error {
fmt.Printf("unload %s, version: 0x%x\n", pluginName, pluginVersion)
return nil
}
func Test(data string) string {
return "hello " + data
}
- build your plugin
go build -buildmode=plugin ./testplugin.go
-
save your testplugin.so to /path/of/plugin/dir
-
write main.go like this
//main.go
package main
import (
"fmt"
"github.com/hepeichun/hotplugin"
)
func main() {
options := hotplugin.ManagerOptions{
Dir: "./",
Suffix: ".so",
}
hotplugin.StartManager(options)
result := hotplugin.Call("testplugin", "Test", "my world")
fmt.Println(result...)
}