Never miss a beat

Join my newsletter.

Tag: go

Using Mongo's ObjectIDs with Go-Graphql

Published: 10/20/2019

I’ve been working on building a Graphql API in Go and recently ran into an issue serializing Mongo’s ObjectIDs. This doesn’t sound like a difficult task, and thankfully it’s not, but it wasn’t super obvious how to go about this. First, we need to identify what an object ID really is. In the official Mongo driver, we can see that there is a type representation for ObjectID. Additionally, there are methods to convert an ObjectID to a hex value and vice-versa. This means that we could maintain two…

Go's Athens Project

Published: 8/26/2019

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…

Testing a Cobra CLI in Go

Published: 7/18/2019

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…

How Golang Interfaces Work

Published: 7/8/2019

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: 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…

Golang: What is a receiver function?

Published: 6/30/2019

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: You could write a function that takes the struct in as a parameter. For example: Go’s receiver function However, Go gives us the ability to specify that getDatabaseRoot is a function that belongs to that Struct…

Prompt the user for a rune at the terminal in Go

Published: 6/13/2019

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…

5 Programming Languages You Should Really Try

Published: 6/28/2017

For some strange reason, the vast majority of my blog readers are Python Developers. I wrote two articles on Python a long time ago and honestly try to avoid python when I can. So here’s an article for you purveyors of Python — you sultans of snakes. Note: This article is still likely relevant even if you’re not a Pythonista . For those who aren’t aware, Linguistics and Grammars are particularly interesting to me. I really, really enjoy looking into new languages and comparing them to what I…