Skip to content

Instantly share code, notes, and snippets.

@chetangiridhar
Last active March 5, 2019 04:56
Show Gist options
  • Save chetangiridhar/1390cbd6dba3e13fe2e73be0a9abda98 to your computer and use it in GitHub Desktop.
Save chetangiridhar/1390cbd6dba3e13fe2e73be0a9abda98 to your computer and use it in GitHub Desktop.
Using Golang plugin
package main
import (
"fmt"
"path/filepath"
"plugin"
)
func main() {
// Glob - Gets the plugin to be loaded
plugins, err := filepath.Glob("math.so")
if err != nil {
panic(err)
}
// Open - Loads the plugin
fmt.Printf("Loading plugin %s", plugins[0])
p, err := plugin.Open(plugins[0])
if err != nil {
panic(err)
}
// Lookup - Searches for a symbol name in the plugin
symbol, err := p.Lookup("Add")
if err != nil {
panic(err)
}
// symbol - Checks the function signature
addFunc, ok := symbol.(func(int, int) int)
if !ok {
panic("Plugin has no 'Add(int)int' function")
}
// Uses the function to return results
addition := addFunc(3, 4)
fmt.Printf("\nAddition is:%d", addition)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment