Craig Weber

Go generics iterator sketch

The new Go generics proposal and playground gives us something to play with. Here’s a sketch of what a basic iterator library could look like. It’s based on function types instead of interfaces; I think this gives better ergonomics than interface-based designs, especially if the proposal drops its seemingly arbitrary restrictions for method types.

I’m not sure about returning a pointer to the type as opposed to a (T, bool) tuple. In particular, I suspect this will cause unnecessary allocations, but I haven’t tested at all.

Read More

Tips for working with multiple GitHub accounts

I use GitHub for my work and personal projects with different profiles for each. Because it’s a good security/privacy practice, each profile has its own distinct SSH key. However, this causes problems because the git CLI will always try to use the first SSH key that maps to the github.com domain even if that key has no permissions for the target repository. The other more straightforward problem with multiple accounts is that the GitHub.com browser cookie asserts that you are only logged into one account at a time.

My solutions for these problems are direnv and Firefox Containers, respectively. These use cases are straightforward applications of these technologies, so I’m not claiming any innovation here, but rather it took me a long time to identify these solutions, and I hope this saves others some time. If you’re not familiar with these tools, read on for details.

Read More

5 myths about infrastructure-as-code via general purpose programming languages

There’s an ongoing debate among infrastructure-as-code practitioners between configuration languages like YAML and HCL and using “real programming languages” (including domain specific programming languages) to generate configuration. The debate is going very poorly because there is a lot of confusion about the “real programming languages” position, so I want to correct some of the common points of confusion (“myths”) so the conversation can focus on more substantial concerns.

Read More

Blog infrastructure updates

Another year, another blog update. BitBucket is deprecating their Mercurial support, and while I really do appreciate Mercurial, it’s just easier for me to keep everything in GitHub than trying to find another Mercurial provider. Also, GitHub seems to be improving at a pretty rapid pace. So voila, this blog is now hosted on GitHub. This includes my pet static site generator, neon which is used to generate this site.

Further, my pet build tool builder is sufficiently far along that I can use it to automate the building of this static site. builder is pretty cool so far. At some point I will write a dedicated post about it.

New blog infrastructure

I finally got around to automating the publishing of this blog. It hasn’t been a high priority, since I only post a couple of times a year, but it’s always bothered me that something that is such an ideal candidate for automation hasn’t been automated. Anyway, I finally did it and I want to describe the setup in case it’s helpful for anyone looking to do the same.

Read More

Deploying Go apps on Docker scratch images

NOTE: If you’re here for the TL;DR, skip to the bottom.

A few months ago I built out some monitoring infrastructure at work using Go. I deployed it to ECS (a Docker orchestrator, functionally similar to Kubernetes), and for fun I decided to see how minimal I could make the image. I’ve used Alpine base images before (which weigh in at about 5 MB and usually another 5 MB for a small Go binary), but being that Go advertises itself as requiring only a Linux kernel (most programming languages depend on an interpreter, a VM, and/or system libraries–the latter abstract over kernel functionality and sometimes provide a stable interface to the kernel), I wanted to see how true or practical this was, and I wanted to better understand the things that I was taking for granted by using distros.

As a matter of context, Docker has a special base image called scratch which is empty–an application running on a scratch base image only has access to the kernel (at least to the extent that containers provide isolation).

Read More

Getting started with Go, 2018 edition

A little over 2.5 years ago, I wrote a tutorial about installing Go. Since then, one of the more significant changes to the Go ecosystem has been the addition of modules, which effectively does away with the hardest part of installing Go–$GOPATH. This change occurred in the latest version: Go 1.11.

In addition to installing Go, I wanted to make a guide that can get you from nothing to a real project in half an hour. Most languages focus their introductory material on the language and briefly cover setting up a toy program. When you’re done, you realize you have no idea how to build a multi-file program, how to add dependencies (or at least how to add them in a way that won’t break other things on your system), how to get an editor up and running, etc.

I’m not going to focus much at all on Go the language here, since it’s super easy to learn and there are already many great tutorials (the official Tour is probably not a bad place to start). I’m only going to go deep enough to give you a lay of the land; if I’ve done my job, it should be easy enough to Google for specific resources on any given topic (for example, testing).

Now without further ado…

Read More

Go's interfaces and nil by example

I’ve recently been involved in conversations with a few Go developers who have expressed frustration about Go’s interfaces with respect to nil. It seems not everyone understands that interfaces are a reference type (like a pointer), and they can reference other reference types (i.e., pointers, maps, slices, etc). Because all reference types have a nil (zero) value, an interface can be nil or an interface can reference a nil pointer; people may fail to realize that the nility of the interface is independent from the nility of the thing it points to, and when we ask (for example) if err != nil, we’re actually asking is the interface nil?, not is the value behind the interface nil?. Here are a few examples that will (hopefully) demonstrate this clearly:

Read More

Benchmarking Go and Python

Sometimes I’m curious about the performance of different languages. At work, I usually write Python, but I often find tasks that are inherently parallelizable and could thus benefit from parallel execution. Unfortunately, Python is notoriously difficult to parallelizea. In one case, we needed to validate that a table of values of a particular type could be convertible into a values of a different type based on some known set of conversion rules. Since Go is a great language for writing concurrent programs (and executing them in parallel), I decided to compare a sequential Python implementation to sequential and parallel Go implementations.

Read More

Installing Go on Linux & OS X

This is a guide for Unix systems (OS X and Linux), but Windows users shouldn’t find it too difficult to figure out the equivalent commands for their platform. I’m not assuming much prior knowledge, but readers should at least be comfortable navigating around a Unix terminal, and any familiarity with environment variables is helpful (a quick Google search for “environment variables” should suffice). Without further ado:

Read More