Codementor Events

How I learned Go (Golang)

Published Dec 14, 2017Last updated Jun 11, 2018
How I learned Go (Golang)

About me

Before learning Go, I had already completed a degree in computer science and began working for a start up company. I have coded in a wide range of languages including Java, C, JavaScript, and Python. I'm writing this as an experienced programmer approaching a new language. If you are someone looking to learn Go as your first language you can start with the tour of go (https://tour.golang.org/welcome/1) to learn the basic aspects of how the language works.

Why I wanted to learn Go (Golang)

I was first introduced to Go while watching videos on Youtube related to development models (specifically related to why Object Oriented Programming had failed to live up to its promises). Go was mentioned as having a loose approach to objects with simple multi-threading tools. Interested, I watched some videos covering the basic syntax of the language. I mentally noted the ease of multi-threading, but moved on to other projects.
I decided to fully dive into Go when I saw that it could be useful for the work I was doing with MAXSET. I had prototyped a system in Python for structuring data and we wanted to now implement it within a compiled language for increased performance and extra protection of IP. Knowing that the prototype used multi-threading I remembered Go. I decided to use this opportunity to learn Go so that I could apply the features of Go in development of the Compiled system.

How I approached learning Go (Golang)

After I decided to learn Go, I first quickly went through the tour of Go at golang.org (https://tour.golang.org/welcome/1). I then began to learn by doing. Since I had a prototype in python to work from, I began by implementing the modules within the python implementation as packages within Go. A big help was the testing module. This module allows you to write test cases for packages. The go compiler has a test command that will compile your source code along with your test cases that then report back what tests your code passed or failed.

Challenges I faced

One aspect of the language that can be difficult to understand is how to handle slices versus arrays. Arrays are like standard C arrays. They are a sequence of adjacent memory locations with a fixed size. Slices are more like Python lists. Internally a slice contains 3 pieces of information: a pointer to an element of an array, the length of the slice, and the capacity of the slice. What can get confusing is that the syntax for arrays and the syntax for a slice are the same. This makes it easy to mix them up. In my experience, unless there is a specific reason you are using an array, use slices for everything. Slices have additional supporting functions that are not shared by arrays, allowing for much cleaner code. What you need to be careful of is that because slices are pointers to arrays, functions can altered the content of slices even though the language is pass by value.

Key takeaways

The Go multi-threading syntax I found very easy to use. How it works is that a new thread is created by using 'go', a reserved word in Go. You also provide a function call after the word 'go' to define the behavior of the thread. Something to note is that the returning within a function that was called with a 'go' will cause the thread to stop executing, but any return value is discarded. In order to get a result from a thread the best way is to provide that thread with a channel to send it's output through. I think of a channel as a pipe between threads. Channels are thread safe so you don't need to do extra work to ensure that multiple threads don't access the same data. I find that the easiest model for setting up threads is to wrap the functions I want performed in worker functions that can receive the input values for the function on one channel and can send the result down another channel.

For example:

package main

import "fmt"

func sqr(x int) int {
  return x*x
}

func main() {
  var in, out chan int
  in = make(chan int)
  out = make(chan int)
  go func() {
    for i := range in {
      out <- sqr(i)
    }
    close(out)
  }
  in <- 1
  in <- 2
  in <- 3
  close(in)
  for o := range out {
    fmt.Printf("I got %d.\n",o)
  }
}

This will output
I got 1.
I got 4.
I got 9.

Rather then creating a new thread for each call to sqr. This method allows the programmer to only create the minimum number of threads required to fully utilize the number of available cores. This decreases the work load on the scheduler allowing your code to operate more efficiently. Also, to ensure that your code won't deadlock all you need to do is make sure that the flow of data through your system doesn't have any loops.

Another powerful feature is the ability to link functions to created types. This is effectively how the language creates objects without needing a programmer to define classes. When you create your own type (either as an alias for a preexisting type, or perhaps a struct of multiple types), you can also write functions, called methods, that are linked to your type. To go along with this there is also the interface type. An interface is a list of function signatures. Any type that implements all the function signatures of an interface automatically can be used as an instance of that interface. By separating the Interface into a separate element from the objects that implement it, programmers can write code in a object oriented manner when it is advantageous, and also write code as independant functions when the complexity of objects isn't required.

Tips and advice

Go as a language has a clean syntax with a lightweight object system. This makes the language both flexible and powerful.

Features that I found unique and appealing to Go include:

Concrete and Reference types for arrays
Concise and straight-forward multi-threading
Interfaces without classes

Once you understand these features of the language everything else operates the same or similar to other popular programming languages.

Final thoughts and next steps

I found Go to be a very easy environment to work in, and I plan to develop future projects in this language when I have the option. To anyone else looking to add this language to their tool box I recommend the tour of Go (https://tour.golang.org/welcome/1). It covers all the basic syntax and an experienced programmer can easily go through it in under an hour. The language is covered under a BSD-style license, so you can easily get all the tools you need to get started at golang.org.

Discover and read more posts from Devon Call
get started
post commentsBe the first to share your opinion
Show more replies