Using Golang plugin
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
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