The Go 1.11 web service Dockerfile

Build with Modules, Ship from Scratch If you use dep, check out this post instead. Goals: The application executable is compiled inside a container, in order to boost reproducibility The resulting image must be as small as possible The application must run in a container as secure as possible: an unprivileged user in a minimal environment The application must be able to make HTTPS calls It is a multistage Dockerfile: the first throwaway stage is used for building, while the final image will only contain the compiled binary executable. ...

November 5, 2018 · Pierre Prinetti
A crash test dummy going to work in a van

Test-Driven Development in Go

In this video, Robert Martin uses Kotlin and JUnit to illustrate his Three Laws of TDD. But what about Go? Follow me and challenge the master! We will walk in his footsteps with the only help of Brad Fitzpatrick’s checkFunc pattern. The Three Laws You are not allowed to write any production code unless it is to make a failing unit test pass. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. You are not allowed to write any more production code than is sufficient to pass the one failing unit test. Prime factors Step 1: the API We want a function to list the prime factors of a given number. ...

July 9, 2018 · Pierre Prinetti

The Go Dockerfile

Build With Dep, Ship From Scratch For Go 1.11 modules, check this post instead. In a devops environment, pushing some code to the repository is not enough. You have to ship it. And the first step is often writing a Dockerfile. The goals: The code has to be compiled in a container, to boost the chances my build will be reproducible. Use dep for fetching the dependencies in case the vendor folder is not committed alongside with the code. NOTE: if vendor/ is in the .gitignore, it should be in the .dockerignore too. The final image should be as small as possible. Go applications compile to a single binary. We can have images as small as our compiled binary by leveraging the special FROM scratch base image in a multi-stage build. Here is my base Dockerfile for Go services: ...

February 14, 2018 · Pierre Prinetti
A dark evening in Berlin. The river borders a small city park park where a distant human figure walks towards a yellow light in the background.

Event Sourcing in Go: the Event Handler

Recently, I have been working on an event-sourced application built around Command-Query Responsibility Segregation (CQRS). My job was to figure out how to implement a new command in Go. In order to act on a given object, we have to build the current state of that object out of the events that created and modified it. Here I want to show how I have come up with the applier interface to represent the Event logic. ...

February 3, 2018 · Pierre Prinetti
A bright red ship propeller in the foreground, steam pipes and machinery in the background.

A pattern for Go tests

I used to spend an unreasonable amount of time thinking about how to begin writing a test. I googled test patterns in Go. Many people seem to rely on external dependencies for assertions. And in fact, I understand that generic (aha!) functions like isNil(v interface{}) bool can initially bring speed to the development. But in the long run, I think that embracing the true strongly-typed nature of Go, instead of just searching for a way around it, is more rewarding. Writing more idiomatic code will be beneficial both for the quality of the code, and for the insights you can get by looking the Beast in the eye. ...

January 28, 2018 · Pierre Prinetti