Earlier in the book, I challenged you to examine each method of DI presented in the book with a critical eye. With that in mind, we should consider the potential costs of monkey patching.
Data races—We saw in our examples that monkey patching is the process of replacing a global variable with a copy that performs in the way we need it to for a particular test. And that is perhaps the biggest problem. Swapping something global, and therefore shared, for something specific causes a data race on that variable.
To understand this data race a little more, we need to understand how Go runs tests. By default, tests within a package are executed sequentially. We can reduce our test execution time by marking our tests with t.Parallel(). With our current tests of the data package, marking the test as parallel would cause the data race to appear, resulting in...