Skip to content

Instantly share code, notes, and snippets.

@Urethramancer
Last active July 5, 2017 16:16
Show Gist options
  • Save Urethramancer/341b879354a01b36ae0ca88e585fbbd6 to your computer and use it in GitHub Desktop.
Save Urethramancer/341b879354a01b36ae0ca88e585fbbd6 to your computer and use it in GitHub Desktop.
Simple plugin example for Go 1.8+.
// addon/addon.go
// The definition of a plugin. Each plugin project would include this stub.
package addon
type Addon interface {
Hello()
}
// hello/hello.go
// Example plugin implementation.
//go:generate go build -buildmode=plugin -ldflags "-w -s"
package main
import "C"
import "fmt"
import "../addon"
type AddonImpl struct{}
var Impl addon.Addon = AddonImpl{}
func (p AddonImpl) Hello() {
fmt.Println("Hello!")
}
// The test program which loads the "hello" plugin.
package main
import (
"plugin"
addon "./addon"
)
func main() {
p := getPlugin("hello/hello.so")
p.Hello()
}
func getPlugin(name string) addon.Addon {
p, err := plugin.Open(name)
check(err)
imp, err := p.Lookup("Impl")
check(err)
return *imp.(*addon.Addon)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment