Dynamic Loading and Symbols
Loading plugins at runtime and calling exported symbols
plugin.Open
plugin.Open(path) loads a compiled .so file. It initialises the plugin's init() functions and package-level variables, then makes symbols available.
p, err := plugin.Open("./plugins/mymodule.so")
if err != nil { log.Fatalf("load: %v", err) }p.Lookup
p.Lookup(name) finds an exported symbol by name. It returns an any; type-assert to the expected type.
sym, err := p.Lookup("NewProcessor")
if err != nil { log.Fatalf("lookup: %v", err) }
fn, ok := sym.(func() Processor)
if !ok { log.Fatal("wrong type") }All lessons in this course
- Go plugin Package Basics
- Designing a Plugin Interface
- Dynamic Loading and Symbols
- Alternatives: HashiCorp go-plugin