My Avatar

(require 'thoughts)

80% engineer, 30% photographer, very good at math.

Random thoughts on working with Go

2016-10-16, published at San Francisco

Last week I had a chat with a friend, who has been pretty upset lately because he was having a hard time to introduce Rust to his company, a hard tech startup and a Go shop. He couldn’t understand why his co-workers would be willing to settle on, quotes from him, “such an ugly tool”.

Fair enough. Admittedly I was never a big fan of Go either. I think the GOPATH is a mess and the dependency management is gnarly; I like the concept of slice, but the operations are pretty weak and almost broken; the variable shadowing is funny and nil interface is somewhat confusing; I don’t really have problem with its error handling like others do, but it does make the program rather verbose… and the list can go on and on.

There might be better alternatives nowadays. I am lucky enough to be able to use Clojure at work. To me, I think Clojure is both beautiful and productive, arguably the best tool for manipulating data, or designing a DSL. It does not really make sense to even try to compare Go with Clojure, these two are just fundamentally so different. For example, Clojure has first-class support for higher order sequence functions, like map, reduce and filter, which are absolutely essential for working with data.

(map odd? [1 2 3])      ;;=> (true false true)
(filter odd? [1 2 3])   ;;=> (1 3)
(reduce + [1 2 3 4 5])  ;;=> 15

It is not that easy, but not too terrible either, to have something similar in Go. Here is an example:

type testInt func(int) bool

func isOdd(x int) bool {
	if x%2 == 0 {
		return false
	return true

func filter(xs []int, f testInt) []int {
	var res []int
	for _, val := range xs {
		if f(val) {
			res = append(res, val)
	return res

Now you can use filter like this;

xs := []int{1, 2, 3, 4, 5}
fmt.Println(filter(xs, isOdd))

Of course, this is a significantly simplified example, it could get cumbersome soon due to the lack of generics in Go (although I never found it being a huge problem of implementing a handful of sort.Interface, but I have not yet used Go in large applications either).

Once you get used to Clojure, everything else just become awkward and odd. However, will I choose Clojure for large-scale web applications? Debatable, probably not. Will I still keep using it in my own projects? Sure. Because despite all the complaints, end of the day I can always count on it to “get the job done”. As a developer writing business software now, I have learned to respect that.