Switching on custom types in Golang

Last week I ran into an unexpected behaviour in Go regarding type switching. In the end, it makes perfect sense but the initial reaction was complete confusion. The algorithm in question needed to perform different types of actions based on incoming messages. Each message could provoke different number of reactions which could take form of different data structures or operations to be performed later. Message handlers therefore returned a slice containing either values of struct A, B or C, or an anonymous function. Read On →

Go race detector

Recently, we developed a web application which involved the use of multiple goroutines. As a result data races were just around the corner. Fortunately, Go comes with go race detector — an incredible tool which makes them easier to detect. Let’s look at a simplified example: var servedDevices []Device devicesToProcess := make(chan []Device, 1) go func(devicesToProcess chan[]Device) { select { case devices := <- devicesToProcess: for _, device := range devices { go func(device Device) { // Operations on Device servedDevices = append(servedDevices, device) }(device) } } }(devicesToProcess) A channel for slices of Devices is passed as an argument to a goroutine. Read On →

I said nil, not nil

Last week, I was working on extending goworker to use Sentinel, a Redis HA solution, instead of plain Redis. The high level goal was clear enough, but the way there was anything but. The root problem which made me go through all stages of programming: that’s easy; huh, strange; WTF?; smashing head agains a wall; and facepalm/double facepalm/n-facepalm turned out to be that nil != nil. At least not always. In this particular instance, I was returnig nil to a pool of Redis connections, when the checked out connection was either dead (= no PONG response) or the instance it was connected to had been demoted to a slave. Read On →

Flexbugs

While updating styles for Enectiva to make it more responsive I came across a problem with flexbox — there are some restrictions in particular browsers. For instance, one issue I encountered was that some HTML elements can’t be used as flex containers. For instance, we wanted to use fieldsets because of their semantics both alone and combined with legends and make them flex containers. This, however, doesn’t work in Chrome. The following example shows that using the same CSS properties (display: flex; flex-flow: row nowrap) the elements inside the fieldset are in a row in Firefox but not in Chrome. Read On →

Breaking in Go

One of Go’s advantages is its familiar syntax and vocabulary. Parentheses, brackets, and braces have the meaning you expect. Programs are composed of well known keywords like var, const, for, or return. This familiarity, however, means that one might forget that Go might have slightly different semantic for those terms. One such example is the keyword break. Recently, we wrote a program which boiled down to an equivalent of: for i := 0; i < 10; i++ { fmt.Println("Line:", i) switch i { case 5: break } } The desired output was to terminate the loop when a condition was reached inside the switch statement. Read On →

Capistrano: changing repository URL

Last week, we moved our git repositories to GitLab so we needed to change all deployment scripts. We deploy using Capistrano and looking at deploy.rb file the task looks simple. Right at the top is something like: set :repo_url, 'user@domain:repo.git' Having learnt from experience, I consulted the docs to verify my assumptions. It was exactly as I thought, I changed the URL, ran a deploy to make sure everything is ok and all permissions are set up properly. Read On →

Beginner's Elm resources

As a newcomer to Elm, I’d like to share a list of resources I’ve found helpful while learning this interesting programming langauge: Official Elm guide - introduction to Elm, detailed, strongly recommended for beginners to understand basics of Elm language, explanation of terms and function Official example applications- very helpful from the beginning too, interactive examples show how the code works in preview, possibility to change the code and see changes on the page Elm community links - very positive to see that you are not alone with Elm. Read On →

git branch --merged

One can explore git and discover something new every day. One of my recent discoveries is the --merged option of git branch. As the name suggests, it allows you to list all branches which have been merged as of a specified commit. Conveniently, it defaults to the current commit, so by running: git branch --merged you can list all the branches which have already been merged up to the current commit. Read On →

Mocking using interfaces

Nearly every piece of software is composed of multiple components which work together to provide a set of functions. The division into components has many benefits, one of which is smaller scope of tests, but to fully leverage it we need to be able to isolate the components in the test environment. Every programming language provides different tools to accomplish this isolation. Let’s look at an example in Go. One of our programs includes a registry of devices represented by a serial number. Read On →

HTTPS in Ruby

Recently, we switched communication between our internal backend services to use SSL/TLS. In cases when have Go talking to Go the switch from HTTP to HTTPS proved very simple. There are, however, instances where Go talks with Ruby. After the success with Go, we prepared everything, set up parallel HTTP and HTTPS endpoints to ease the transition and limit the downtime. Everything went well until we switched Ruby code to use the HTTPS endpoint. Read On →