How to Simulate Monkey Patching in Go Using Interfaces and Reflection

The article explains why Go does not support traditional monkey patching, then demonstrates two practical approaches—using interfaces with wrapper types and employing reflection—to extend a SimpleLogger with log levels without modifying its source, while discussing trade‑offs such as readability and performance.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
How to Simulate Monkey Patching in Go Using Interfaces and Reflection

Introduction

Monkey patching is a technique that modifies or extends the behavior of a library or object at runtime without changing its original source code. While useful in dynamic languages like Python, it is generally discouraged because it can make code harder to understand, maintain, and upgrade.

Go is a statically typed, compiled language that does not directly support monkey patching. Its design philosophy favors explicitness and simplicity, encouraging the use of interfaces and composition for extensibility and testability. Nevertheless, Go developers can achieve similar effects through creative use of interfaces, reflection, and wrapper types.

Using Interfaces to Achieve Monkey‑Patch‑Like Behavior

Define a logger interface and a simple implementation:

type Logger interface {
    Log(message string)
}

type SimpleLogger struct{}

func (l SimpleLogger) Log(message string) {
    fmt.Println(message)
}

Create a new type that embeds the original logger and adds a log level:

type LevelLogger struct {
    logger Logger // embed Logger interface
    level  string
}

func (l LevelLogger) Log(message string) {
    l.logger.Log(fmt.Sprintf("[%s] %s", l.level, message))
}

Using Reflection for Dynamic Modification

Reflection allows runtime inspection and invocation of methods. Although powerful, it can reduce readability and performance, so it should be used sparingly.

Below is a function that wraps any Logger with a log‑level decorator using reflection:

func AddLogLevel(logger Logger, level string) Logger {
    return &levelLogger{logger: logger, level: level}
}

type levelLogger struct {
    logger Logger
    level  string
}

func (l *levelLogger) Log(message string) {
    // Dynamically call the original Log method via reflection
    reflect.ValueOf(l.logger).MethodByName("Log").Call([]reflect.Value{
        reflect.ValueOf(fmt.Sprintf("[%s] %s", l.level, message)),
    })
}

Example usage:

func main() {
    logger := SimpleLogger{}
    loggerWithLevel := AddLogLevel(logger, "INFO")
    loggerWithLevel.Log("this is an info log")
}

The AddLogLevel function returns a new Logger implementation that, when its Log method is called, first adds the specified level and then forwards the call to the original logger via reflection.

Caution and Best Practices

While reflection can simulate monkey patching, it introduces performance overhead and can make code harder to understand. Prefer Go’s native mechanisms—interfaces and composition—whenever possible, and reserve reflection for cases where other approaches are impractical.

Conclusion

Go does not provide built‑in monkey patching, but developers can safely extend functionality using interface‑based wrappers or, when necessary, reflection. These techniques maintain type safety and keep code maintainable, provided they are applied judiciously.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Design PatternsGoReflectionInterfaceMonkey Patching
Ops Development & AI Practice
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.