Don’t use mocks

Writing good unit tests is made much easier by dependency injection. This lets you separate your code’s behavior from that of your dependencies.

Many people use mocks to add dependencies to unit tests. I think this is usually a mistake.

Read more →

Looking in Go’s Mirror: How and When to use reflect

This article was originally published on gopheradvent.com Looking in Go’s mirror: How and when to use reflect Go’s static typing is a headline feature of the language. It prevents whole classes of bugs, makes code easier to navigate and refactor, and makes it easier for linters to analyze. But there are times when it’s very constricting. What if we’re reading JSON files from disk with unknown structure? We can’t define a type ahead of time that will cover all cases.
Read more →

Nasty refactorings with go.mod replace

At work I’ve been building a new program on top of an SDK that’s under very active development. After about 6 weeks without updating the version, the SDK had deleted some code I was using and had a ton of breaking changes. If I’d simply updated the library, my entire program would fail to build, and it also wouldn’t be clear how to get it back to a good state. Following in the spirit of Branch by Abstraction, I’d rather introduce the new code side-by-side with the old and incrementally migrate without breaking the program.
Read more →