Why I’m So Frustrated WithÂ Go
Tags: go
Published: 2017-06-01 16:09:19

---
title: Why I’m So Frustrated WithÂ Go
published: true
description: A brief example demonstrating my current frustrations with the Go programming language.
tags: Go, golang
---

It’s been about a year since I started using Go. I’ve written a lot of code with it, including an entire micro-service and a contribution to glide. I want to emphasize that, for the most part, it’s been pretty good. But about once a month, my eye starts twitching and I get the urge to start making a head-to-desk motion. The reasons for this can be pretty simply demonstrated. Since I seem to have this conversation on a regular basis, I thought I’d record my thoughts so I can just simply reference this the next time it comes up.

# A Simple Data Structure
I need a data structure. It has the following requirements:

1. It must store the name of an animal and an associated struct containing some information regarding the animal.
1. It must have O(1) lookup time.
1. I must be able to iterate over it’s elements in some predefined order.
1. It must be immutableÂ¹.

At first glance, we might be tempted to simply use a map like so:
```Go
type AnimalMap map[string]Animal
```

This is kinda nice. In fact, it satisfies the first two requirements of our data structure. But since the very early days of Go, the [order objects are stored in a map is random][maporder] (for good reason). So I can’t do something simple like insert all the elements into the map in the same order over which I’d like them to be iterated.

Alright, no big deal. We can get around this by just writing a little bit more code, using an array to keep track of order.

```Go
type AnimalMap struct {
  Mapping map[string]Animal
  Order []string
}
```

Now whenever we want to iterate over the map, we need to iterate over the `Order` slice instead, using the strings we pull off as keys into the map. Not the greatest, but it works.

# What About Immutability?
This is where things get really hairy. Go doesn’t have the concept of [runtime constants][rconst]. If we want things to be immutable after we create them, we’re going to have to completely, and I mean completely, encapsulate both the slice and the map in our struct. This is because maps and slices have [pass-by-reference semantics](https://stackoverflow.com/questions/40680981/are-maps-passed-by-value-or-by-reference-in-go). In other words, if at any point I give you a reference to them, you’ll be able to modify them. The best I can come up with is now this explosion of code:

```Go
type Animals interface {
	Len() int
	At(i int) string
}

type animals []string
func (a animals) Len() int {return len(a)}
func (a animals) At(i int) string {return a[i]}

type AnimalMap struct {
	mapping map[string]Animal
	order   animals
}

func NewMap(order []string, mapping map[string]Animal) AnimalMap {
	newMap := AnimalMap{
		mapping: make(map[string]Animal, len(mapping)),
		order:   make(animals, len(order)),
	}

	for i, name := range order {
		newMap.mapping[name] = mapping[name]
		newMap.order[i] = name
	}

	return newMap
}

func (am AnimalMap) Order() Animals {return am.order}
func (am AnimalMap) Animal(name string) Animal {return am.mapping[name]}
```

What just happened? That’s an infuriating amount of code to write. You know how much code I’d have to write if this were [C++][cppmap], [C#][csharpmap], or [Java][javamap]? None. They all have reusable notions of an immutable, ordered map. Yet in Go, every time I need one I have to write 30 lines of code. I feel like I had to fight to Go every step of the way here. I feel sad.

# Conclusion
Go has some pretty neat concepts. I love that it enforces composition over inheritance. Goroutines are pretty nifty. I’ve been pretty satisfied with it overall. But time and again, I find myself solving the same problems over and over when using Go. I find myself completely blocked, with no way to write DRY codeÂ².

I’d love to see the Go community address issues like this. IMHO generics would solve all of the issues above, but I understand that’s somewhat of a sore topic for the Go community. Perhaps something like a simple form of codegen for common data structures and algorithms?

***

*Kurtis Nusbaum is a Mobile and Backend Developer at Uber. He’s an avid long distance runner and diversity advocate. Here's his [mailing list](http://tinyletter.com/kurtis). Here's his [Twitter](https://twitter.com/klnusbaum), and [LinkedIn](https://www.linkedin.com/in/knusbaum). Here’s his [github](https://github.com/klnusbaum). If Kurtis seems like the kind of guy with whom you’d like to work, shoot him an e-mail at kcommiter@gmail.com.*

***

Â¹Here's [why you might want immutability][immut]
Â²For instance, try writing a reusable exponential backoff algorithm or a reusable binary tree. Oh, and you can’t use interface{}. We’re programming in a statically typed language for a reason.

[immut]: https://softwareengineering.stackexchange.com/questions/151733/if-immutable-objects-are-good-why-do-people-keep-creating-mutable-objects
[maporder]: https://blog.golang.org/go-maps-in-action#TOC_7.
[rconst]: https://www.quora.com/What-is-the-compile-time-constant-and-run-time-constant-in-the-C-programming-language
[cppmap]: https://stackoverflow.com/questions/2636303/how-to-initialize-a-private-static-const-map-in-c
[csharpmap]: https://msdn.microsoft.com/en-us/library/dn467194(v=vs.111).aspx
[javamap]: https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/ImmutableMap.html