Last active
July 5, 2017 16:16
-
-
Save Urethramancer/341b879354a01b36ae0ca88e585fbbd6 to your computer and use it in GitHub Desktop.
Simple plugin example for Go 1.8+.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// addon/addon.go | |
// The definition of a plugin. Each plugin project would include this stub. | |
package addon | |
type Addon interface { | |
Hello() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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