Learnings from my first Gojo

Stefan M.
Golicious
Published in
3 min readMar 6, 2019

We at grandcentrix do more or less regularly coding dojos. Last week (or probably not anymore when you read this 😅) I attended our first Gojo (Golang coding dojo) hosted by my colleague Camal Cakar.

As the past shows — the first dojo is always more like a get together and start to learn each other instead of coding that much. But beside of this I learned basically three things there:

  1. The basics of testing
  2. A thing called struct
  3. Golang has pointers

Testing basics

A test file should be named like the file you want to test but with a _test suffix. Given our implementation file is named tennis.go, our test file has to be named tennis_test.go. In addition to that the package should be the same as well. When we have this set up we can import the testing package (I still don’t get where these packages came from or where I can find documentation about this). This can be used to tell go that our tests have failed. A public function which has to be prefixed with Test* and a parameter of the type *testing.T (whatever this is) is also necessary, so the test is detected by the go command line tool.

And the end a generic test looks like the following:

package [Package_To_Test]import(
“testing”
)
func Test[Something](t *testing.T) {}

Now we can start to fill the Test* function and write the actual test. To note here is that even if one of the SUT functions is private it can be accessible by the test. This is pretty awesome and totally different to Kotlin!

Here is an example of a fully working test:

func TestAddShouldReturnTheSum(t *testing.T) {
want := 4
got := add(2,2)
if want != got {
t.Errorf(“%d is not %d”, want, got)
}
}

It is common for go programmers to use want, got and a simple if statement in tests.

The fully working code of this is available in this gist at GitHub.

The struct type

You may know that Golang is not really object orientated. If not — well, now you know 😅. That means that there don’t exist classes like a Set or a List in Kotlin. But you can create a struct. A struct is something similar to a List… or maybe to an Object to be more generic. Because a struct holds just different types of data.

So given we want to create a player — for a tennis match for example — we can create a struct with the name player and give it some fields:

type player struct {
name string
score int
}

The rules of private and public types are the same as for functions! So lower case means private and upper case public. Our player can also be used in our file only.

To create an “instance” of this we only have to “name it” followed by curly brackets:

playerOne = player{ }

This is a little bit similar to named constructor parameters in Kotlin. Because we can not only create an “empty” player (with the default values for the fields) — we can also create a new player and set the fields directly. Because a struct is mutable (sadly 😞) you can also set only the fields you want.

With this information all of the following is possible:

aPlayer := player{ }
aPlayer := player{ "Stefan", 15 }
aPlayer := player{ name: "Stefan", score: 15 }
aPlayer := player{ name: "Stefan" }
aPlayer := player{ score: 15 }

You can then access each of their fields with aPlayer.[field]. Either by setting them (because of their mutability) or getting them.

Pointers

Go has pointers. But this is basically all what I learned. Unfortunately, my experiences are with pointers are non-existent. So I don’t really know the difference between pointers to… no pointers(?!) 😄. But at least I know that they exist and I have to write this down to my “have to learn” list.

Learning sources

I learned this in the Gojo. We have also learned about the struct there. But we used this site to learn about it.

--

--