This week, I moved from reading the Go code to changing it. My goal was the first small step of the C++ to Go migration: get the existing Go version of the registration system to build cleanly, and make sure it can’t damage real data when it runs.

Getting It to Build

The Go version didn’t build from a fresh copy. It was missing go.sum, a file that stores a fingerprint of each outside library the code uses, so Go can check it downloaded exactly the right versions. Once I generated go.sum, the build still failed, because the Go files sat in the same folder as the old C++ files and Go refuses to build that mix. I moved the Go files into their own folder, which is where Go projects usually keep a program’s starting code. After that, both the build and Go’s built-in code checker passed.

Fixing How It Handles Records

The registration system watches the database and reacts whenever a record changes, such as a new registration, a settings change, an export request or an order. Before it can react, it first has to work out which of these types the record is, because each type is handled differently. I found three problems in this part. An error on a single record shut the whole service down. Records it didn’t recognise were deleted, including the database’s own internal records. And an order with no status crashed the program. I moved the step that works out a record’s type into its own small function, separate from the rest of the code. Then I changed the service to log errors and carry on, to leave unknown records alone, and to treat a missing status as empty instead of crashing.

Testing Without a Screen

On the frontend I could always click through a page to check my work, but a backend has no screen. Instead, I wrote my first Go unit test, a small program that feeds fake records into a function and checks the answer. I could only do this because I had moved the step that works out a record’s type into its own function. Before, that step needed a live database before it could run. The test covers ten kinds of records and all of them pass.

Next Week’s Plan

Next week, I will add tests proving that unknown records are no longer deleted and that orders without a status no longer crash.

Categories: Experiential

0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.