Into the Golang land: part#1

It was time for my next timeboxed 2h of learning tech. This time I decided to look at golang and some of the features. As with python, I have started from understanding some basic tooling and practices.

Uber had created a pretty good language guide. One of the first shockers for me, was that a tool similar to isort in golang – goimports, works only on save hooks in GUI-based editors. Hard to make it to work from a console / pipeline.

The intent for me was to learn about standard tools, naming conventions and how to create modules. I was writing some go in the past, but without this upfront preparation.

At last, the secrets of golang are revealed

I have created a repository here. Based on a simple problem of wordcount.

I wanted to add a linter, unit tests and code formatter. The first thing I learned though was that my environmental variables were not set correctly. After quite a bit of research, I’ve ended up adding these to PATH:

export GOPATH=$HOME/go
export GOBIN=$HOME/go/bin
export PATH=$PATH:$HOME/go/bin

This allowed me to finally download the right packages and add the right tools to Makefile.

Then I moved onto trying to package the kata itself to be an importable library to the main package. I won’t lie, this took me another 2h (and breached my kata timebox). After numerous issues with local imports working, but linter complaining, I have decided to commit my repo to GitHub and source the module from there. I have not figured out yet how to tag it with appropriate version, so what you’ll see in the repo is an auto-generated version based on date and latest commit hash to main. I got slightly annoyed with packages / modules in Golang and I believe handling of these could be improved (compared to how easy it is for a newcomer in java or python). Might be because Golang was initially built as a language, that shouldn’t require many external packages, this feature was not prioritized.

Note: My learning on golang packages and modules started here: I’d recommend anyone to read it, though it was not the end of the journey.

With the packages working, I thought to myself – why not try now to use docker. Sooner or later (like in my next kata), I will want to run some app in docker. Finding a good Golang docker image is not that hard. A bit of fiddling and it was there.

At this point I said – my tests are looking a bit unchallenging. What could be the worst and evilest strings I could push to my program to have some fun? So I found a big list of naughty strings. Test cases can become interesting with these. See below.

func TestShouldCountNonWhitespaceC0Controls(t *testing.T) {
	assert.Equal(t, WordCount(" a"),
		map[string]int{"\x01\x02\x03\x04\x05\x06\a\b\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\u007f": 1,
			"a": 1})

With all of that and pride of some accomplishment made, I decided it was enough for my kata.

So in this episode, I have learned:

  • how to deal with packages and modules (partially, still more to learn)
  • linter and formatter (golint, go fmt)
  • how to setup Golang environment
  • how to use docker Golang images
  • quite a bit on evil/naughty string inputs
  • about go vet (which did not detect any issues, as my program is really simple, but might be of use in the future)
  • about using maps in Golang

And here is a video I watched upfront:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s