0Pricing
Go Academy · Lesson

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

  1. Go plugin Package Basics
  2. Designing a Plugin Interface
  3. Dynamic Loading and Symbols
  4. Alternatives: HashiCorp go-plugin
← Back to Go Academy