Skip to content

Instantly share code, notes, and snippets.

@chetangiridhar

chetangiridhar/main.go

Last active Mar 5, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.