Lab 12: Plugin System

Time: 45 minutes | Level: Advanced | Docker: docker run -it --rm golang:1.22 sh

Overview

Build a plugin system using Go's plugin package (-buildmode=plugin) and the hashicorp/go-plugin RPC-based alternative. Learn plugin contracts, limitations, and production patterns.


Step 1: Go Plugin Basics

Go Plugin Architecture:
  Main App ──► plugin.Open("greeter.so") ──► plugin.Lookup("Plugin")
                                               ──► type assertion to interface
                                               ──► call methods

Limitations of plugin package:

  • Requires CGO + glibc (not Alpine/musl)

  • Plugin and host must be compiled with same Go version

  • Plugin cannot be unloaded

  • Not supported on Windows

💡 Use docker run -it --rm golang:1.22 sh (Debian/glibc) instead of Alpine for plugin support.


Step 2: Define Plugin Contract


Step 3: Build a Plugin


Step 4: Build Another Plugin


Step 5: Host Application


Step 6: Full Verified Demo (glibc Docker)

📸 Verified Output:


Step 7: hashicorp/go-plugin (Production Alternative)

hashicorp/go-plugin solves the limitations of the stdlib plugin package by running plugins as separate processes with RPC.


Step 8: Capstone — Plugin Registry

Run the demo:

📸 Verified Output (stdlib plugin.Open on glibc):


Summary

Feature

stdlib plugin

hashicorp/go-plugin

Transport

Shared memory

RPC (net/rpc or gRPC)

Isolation

None (same process)

Process isolation

Cross-language

No (Go only)

Yes (any language)

Platform

Linux/macOS only

All platforms

Unload

No

Yes (kill process)

Version mismatch

Panics

Negotiation support

Use case

Same-Go-version, Unix only

Production plugins

Key Takeaways:

  • plugin.Open requires CGO + glibc; Alpine/musl is not supported

  • Both host and plugin must be compiled with identical Go version and flags

  • Interface-based contracts make plugin type assertions safe

  • For production: use hashicorp/go-plugin for isolation and cross-language support

  • Alternative: embed Lua/JavaScript (tengo, goja) for scripting without plugin limitations

Last updated