1. How does monkey patching work?
At its most basic level, monkey patching in Go involves swapping out one variable for another at runtime. This variable can be an instance of the dependency (in the form of a struct) or a function that wraps access to the dependency.
At a higher level, monkey patching is about replacing or intercepting access to a dependency to replace it with another implementation, typically a stub or a mock, to make testing simpler.
2. What are the ideal use cases for monkey patching?
Monkey patching can be used in a variety of situations, but the most notable are the following:
- With code that relies on a singleton
- With code that currently has no tests, no dependency injection, and where you want to add tests with a minimum of changes
- To decouple two packages without having to change the dependent...