Go’s Athens Project

I recently got back from Gophercon in San Diego and am still excited about the talk on the Athens Project. For those unaware, the Athens Project is a module proxy for Go! It’s currently in beta, but showing a ton of promise already. Go’s dependency management has changed a lot over the past few years. For simplicity, Go allowed you to simply specify your dependencies in your code as an import. go get would then get all the dependencies that your application needed. This worked, for a while, but as dependency versions began to churn, we began to see issues...

A Beginner’s Guide to Java Enums

An Enum in Java is a special data type that encompasses a set of predefined constants. When setting a variable of that Enum type, you have to use one of the constants that you define for it. A great example is the compass: public enum Compass { NORTH, EAST, SOUTH, WEST; } Compass direction = Compass.NORTH; In the above example, we create a new enum and it’s predefined set of constants. Then we create a variable of that enum type, and assign it to one of the constants. Keep in mind that we can only set our direction to Compass.NORTH,...

Testing a Cobra CLI in Go

Go has a fantastic library for writing CLI’s (Command Line Interfaces) called Cobra. I’ve been working on a CLI named Deckard for a few months now. Being new to Go, I had (lazily) shied away from writing tests. However, after thinking about my test plan and doing a little refactoring, I’ve found a great way to handle testing your Cobra CLI application. The idea behind Cobra is that you simply write “Command” functions. These command functions are then called by the Cobra library when it parses a valid command. This means that Cobra handles a lot of the heavy lifting...

How Golang Interfaces Work

Interfaces are a tool that allows you to define the behavior of objects and in Golang, interfaces are no different (except that they work on structs instead of “objects”). However, Go has some strange features for interfaces that users from other languages might not expect. Let’s take it slow and start by defining an interface in Go: type Sandwich interface { BeEaten() CountPickles() } This fairly simple interface defines what a sandwich is capable of. Not what it looks like, not how it’s composed, but for the purposes of our application it’s all that we need to constitute a sandwich....

Golang: What is a receiver function?

Classes aren’t really a thing in go, so you cant have instance methods (like Java or similar), however, you may have noticed some functions in Go that appear to be instance methods. These are Go’s receiver functions. The way they work is quite simple. If you have a struct like so: type Database struct { Host string Port int User string Password string Dbname string Driver string } You could write a function that takes the struct in as a parameter. For example: d := Database{...} func getDatabaseRoot(db *Database) { return db.Host + ":" + db.port } getDatabaseRoot(d) Go’s receiver...

Generating HTML from a List in Elm

Elm is a fantastic language for building web applications. It provides a rich DSL for writing HTML that functions in a similar way to JSX (but still quite different). Overall, I enjoy writing it, however, I often forget how to generate HTML from a list of data in Elm. Indeed, the problem is not that difficult, and once you approach it from a functional mindset, it’s easy to see how it works. A simple, yet somewhat contrived example, can be found below: module Main exposing (main) import Browser import Html exposing (Html, div, text) type alias Model = { rows...

Prompt the user for a rune at the terminal in Go

I’ve been spending a lot of my spare time working on a Go project called Deckard. It’s a command line interface (or CLI) for handling database schema changes. Recently, I found myself wanting to prompt the user for a rune before running a command in that CLI. Why would I do this? Deckard handles schema changes via the migrations pattern. This consists of having an up migration and a down migration. The up migration might be to add a new table or alter a column, while a down migration might be to drop that table or alter that column back...