My second Gojo — here are my results

Stefan M.
Golicious
Published in
4 min readMar 28, 2019

Four weeks ago I have attend my first Gojo (Golang coding dojo). We do this every two weeks. Therefore two weeks ago I’ve learned something new 🎉. Time to share what I learned…

This time our dojo moderator decided to have a “simpler task”. Last time we struggled a little bit with the task. Not because the task was that difficult but more because most of us are not that experienced in the go programming language. Nevermind, all I want to say is that we were able to code more than last time 🙂.

My learnings are:

  • There is an interface in go
  • How to Unmarshal (json) bytes into a struct
  • Exploring the slice type
  • Similar to Kotlin you can use range 🤯
  • I “halfway” understand when to use defer
  • new() can be used to allocate memory

Interface

Golang has — similarly to the Java/Kotlin world — an interface type. An example of an interface implementation is error:

type error interface {
Error() string
}

During the dojo I didn’t really know what that means and didn’t have the time to investigate of it. Probably there will be another post in the future which explains it further…

json to struct

We already know what a struct is and how to declare public and private fields. For example the following people struct:

type people struct {
name string
}

Now we want to Unmarshal a json string to this people struct. We can do this with the (contained in the encoding/json package) function Unmarshal. But before we do this we have to change our struct to the following:

type people struct {
Name string `json:"name"`
}

What we did is to change the private name field to public. This is required because otherwise the encoding/json package would not be able to access (set) the field. Also we added the crazy `json:"name"` thing after the field declaration. To be honest I don’t know “what it is” (a comment or something 🤷‍♂) but this is required for the Unmarshal function to map the json value with the key name to the struct field.

When we have done this changes to our struct we can do the following:

jsonString := “{ \”name\”: \”Stefan\” }”
jsonBytes := []byte(jsonString)
peopleStruct := people{}
fmt.Println(peopleStruct.Name)
json.Unmarshal(jsonBytes, &peopleStruct)
fmt.Println(peopleStruct.Name)

The Unmarshal function takes a struct pointer to set the jsonString values to the struct.

Slice it

I don’t want to go too deep into this topic because I have already planned another “More Golang types: Array, Slice and Map” post. But to give you a short hint about it: It is similar to a List in Kotlin. You can create a slice and append and remove values from it.

forEach in go

When we want to iterate over a Collection in Kotlin we can use the forEach function. The passed lambda will be invoked for each item in the Collection with the item as parameter.

Unfortunately something as easy is not available in Golang. But we can make use of the for loop (like we have done in the good old Java 6 days 😅) and the range keyword for it:

slice := []string {"S","t","e","f","M","a"} 
for i, char := range slice {
fmt.Printf("%d is %s \n", i, char)
}

The range keyword will return the index and the item of the (in this case) slice.

When to use defer

In one of my latest post I explained you the defer keyword. But back then I wasn’t really sure when I should use it. I only learned that it exists.

In the Gojo we had a quite nice discussion about this. What we have done is to defer to Close the Body of an http.Get Response. I asked the attendees why we do that and why this is necessary. The response was something like:

When we close the body directly we can’t read the body content anymore. But on the other hand, when you don’t close the body you will leak (a resource) on the machine. Because the file description can’t be freed.

I think these three sentences explain it very well.

The new() function

I was so happy back then as I switched from Java to Kotlin because Kotlin just removed the new keyword to create a new instance of an object. Seems it is back in Golang 🙂.

But no worries. It is not that often used — at least from what I’ve seen so far — like in Java. Anyway. The new() function can be used to allocate memory for a type. Meaning instead of creating an “empty” struct (like we have done with the people in the json to struct section above) we can say new(people).

Learning sources

I have learned this in the Gojo. Of course I also browsed the Golang documentation while writing this article for a quick recap 🙂.

--

--