How to Build a Name Injection Registry in Go for Flexible Plugins
This guide walks through creating a name‑based injection system in Go by defining a processor interface, implementing concrete processors, building a registration map with init functions, and demonstrating usage in a main program, while also covering best practices such as global variable handling, initialization order, and error checking.
Background
In Go, third‑party libraries often register themselves during package import using init functions and global variables. This article explains how to create a similar name‑based injection mechanism.
Implementation Steps
1. Define interface and implementations
package processor
import "fmt"
// Processor defines the processing interface
type Processor interface {
Process(data string) string
}
// UpperCaseProcessor implements Processor, converting data to uppercase
type UpperCaseProcessor struct{}
func (p UpperCaseProcessor) Process(data string) string {
return strings.ToUpper(data)
}
// LowerCaseProcessor implements Processor, converting data to lowercase
type LowerCaseProcessor struct{}
func (p LowerCaseProcessor) Process(data string) string {
return strings.ToLower(data)
}2. Create registration mechanism
package processor
import (
"fmt"
)
var (
processors = make(map[string]Processor)
)
// RegisterProcessor registers a processor implementation under a name
func RegisterProcessor(name string, processor Processor) {
if processor == nil {
panic("processor: Register processor is nil")
}
if _, dup := processors[name]; dup {
panic("processor: Register called twice for processor " + name)
}
processors[name] = processor
}
// GetProcessor retrieves a processor by name
func GetProcessor(name string) Processor {
processor, ok := processors[name]
if !ok {
return nil
}
return processor
}3. Register implementations in init functions
// uppercase.go
package processor
func init() {
RegisterProcessor("uppercase", UpperCaseProcessor{})
}
// lowercase.go
package processor
func init() {
RegisterProcessor("lowercase", LowerCaseProcessor{})
}4. Use the registered processors
// main.go
package main
import (
"fmt"
"log"
"path/to/your/package/processor"
)
func main() {
upper := processor.GetProcessor("uppercase")
if upper == nil {
log.Fatal("Processor not found: uppercase")
}
fmt.Println(upper.Process("hello world"))
lower := processor.GetProcessor("lowercase")
if lower == nil {
log.Fatal("Processor not found: lowercase")
}
fmt.Println(lower.Process("HELLO WORLD"))
}5. Run the program
HELLO WORLD
hello worldBest Practices
Use global variables wisely : they are essential for the registry but should be managed to avoid naming conflicts and unwanted state.
Initialization order : ensure packages initialize in the correct order to prevent subtle bugs.
Error handling : check for nil returns from GetProcessor and handle errors appropriately.
Conclusion
The example demonstrates a simple name injection mechanism that enables registration and retrieval of implementations by name, useful for plugin systems or strategy patterns, and improves code flexibility and maintainability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
