How-Tos

5 min read


JSON Tricks: "Slightly" Custom Marshaling

For more content like this, buy my in-progress eBook, Data Serialization in Go⁠, and get updates immediately as they are added! Have you ever found yourself writing a custom JSON marshaler in Go, because you needed something only slightly different than what the standard JSON marshaler provides? Maybe the consumer of your JSON payload expects an array where you have a single item. Or maybe you need to nest your object one level deeper in your JSON than is used in your application.

Testing

10 min read


Simple Go Mocks

Go’s interfaces and “duck typing” makes it very easy to create simple mock or stub implementations of a dependency for testing. This has not dissuaded a number of people from writing generalized mocking libraries such as gomock and testify/mock, among others. Here I want to describe a simple alternative pattern I frequently use when writing tests for an interface, that I think is generally applicable to many use cases. No Silver Bullet Of course neither this approach, nor any other, is a one-size-fits-all solution.

Other

2 min read


How I got go-spew to work with GopherJS

go-spew is a very handy library used for dumping arbitrarily complex data structures in a (roughly) human-readable format. This is immensely helpful when debugging or writing automated tests in programs. Coupled with a package like go-difflib, it can make comparing the expected and actual results of a test not only easy, but into something approaching fun. Much of my time lately is spent hacking on projects to be compiled by GopherJS, the Go-to-JavaScript compiler.