Go plugin Package Basics
Building .so files and plugin.Open / Lookup
Go plugin Package Basics is a free Go Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
What is the Go plugin package?
The plugin package (Go 1.8+) allows loading compiled .so shared libraries at runtime and calling exported functions and variables from them.
Building a plugin
Compile a Go file as a plugin with -buildmode=plugin:
// greeter/greeter.go
package main
func Greet(name string) string { return "Hello, " + name }Loading a plugin
Load the .so file and look up exported symbols:
p, err := plugin.Open("./greeter.so")
if err != nil { log.Fatal(err) }
sym, err := p.Lookup("Greet")
if err != nil { log.Fatal(err) }
greet, ok := sym.(func(string) string)
if !ok { log.Fatal("wrong type") }
fmt.Println(greet("World"))Plugin constraints
All packages imported by the plugin and the host must be identical versions. The plugin must have package main. Plugins cannot be unloaded once loaded.
Symbol types
plugin.Lookup returns an any. Type-assert it to the expected function signature or variable type. Mismatches cause runtime panics.
Platform limitations
The plugin package works on Linux and macOS but not on Windows or with CGO_ENABLED=0. It also does not support cross-compilation to plugin targets.
Version compatibility
Plugin and host must be compiled with the same Go version and the same versions of all shared packages. Version mismatch causes the plugin to fail to load.
Use cases
Plugins suit: content management systems with user-supplied handlers, databases with swappable storage backends, and applications that need runtime-loadable extensions without recompiling the host.
Alternatives
For more flexibility, use RPC-based plugins (HashiCorp go-plugin), WebAssembly modules, or a scripting language (Lua, Starlark) embedded in the host instead of native plugins.
Debugging plugins
Plugin symbols not found? Check: correct package main, exported (capitalised) symbol, matching build tags and module versions, correct platform.
Plugin loading is one-way
Once a plugin is loaded, it cannot be unloaded. Design systems to tolerate all loaded plugins being active for the lifetime of the process.
Quick Check
What is a key limitation of the Go plugin package compared to HashiCorp go-plugin?
Recap: Go plugin Package
Key points:
- Build with -buildmode=plugin; load with plugin.Open
- plugin.Lookup returns any; type-assert to expected type
- Linux/macOS only; identical versions required; no unloading
- Prefer go-plugin (RPC-based) for cross-platform flexibility
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Go plugin Package Basics” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “Go plugin Package Basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
What will I learn in “Go plugin Package Basics”?
Building .so files and plugin.Open / Lookup You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Go plugin Package Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Go plugin Package Basics
- Designing a Plugin Interface
- Dynamic Loading and Symbols
- Alternatives: HashiCorp go-plugin