Learning Go

Chico Pimentel
re_type
Published in
4 min readNov 23, 2020
trees and palindromes

After some research and talks with friends, I came to this decision: Go would be my next programming language of choice. By the way, Go is the language and Golang refers to the language domain (golang.org). Colloquially both names refer to the Go programming language. The language is also amongst the most loved and wanted based on the 2020 stack overflow survey. I hope this article can help you with ideas and references.

Summary of this article

  1. Learning path: my learning style for programming languages
  2. The good stuff: things I’ve personally enjoyed in Go
  3. The not so good stuff: things I haven’t enjoyed that much
  4. Practical examples: some basic practical examples with learning purpose

Learning Path

As any developer with some industry background. It’s pretty common to move by choosing preferable learning paths. You know, the one that works better for you, given adaptation and routine. My choice is always to:

1. Read books
2. Do some code implementation

Before starting I've moved on and asked some colleagues about book recommendation. A specific answer of a colleague got my attention:

“I haven’t read any book, my learning was organic” 

Organic is definitely an overused word nowadays, at lest in Brazil. But anyway, it's easy to read in between lines. This is just a different learning path than mine, someone that doesn't enjoy technical books that much.

I went on and picked a well rated book about Go. And the one of choice was: “The Go Programming Language”. It’s a pretty nice starting book. I just didn’t have time to complete all code challenges, some were quite big, some didn’t have much details and specs. So it was worth for me to read, get the information, pin the most relevant pieces and apply it.

After some small implementations I've come to some conclusions about the language.

The good stuff

Code style is not a choice, it’s decided by Go.

No time is lost choosing the best coding style, it's already decided and the gofmt is almost a default styling tool. By code style I mean source file formatting.

The compiler also enforces all dependencies and variables have to be used. This makes a cleaner code, no unused dependency or var will be forgotten.

If you're looking for broader coding styles like convention, check out the effective go or uber-go sources.

Small Syntax

This is very nice, there's not much things to remember. There's smaller code variation. This helps a lot to read other people code. And, you can invest your brain power in solving a problem and less about the syntax choices.

Assign values together with types

For a statically typed language I found this to be very helpful. You don't have to write a lot of code just regarding variable typings.

val := 1 // assigning and creating an int type var

Remember to be a little careful, assigning together with type will always create a new variable. This following code situation will lead to an infinite loop: the variable a outside the loop never change and a inside the loop will always be ["b", "c"]

a := []string{"a", "b", "c"} // a will never change
for len(a) > 0 { // len(a) is always 3
val, a := a[0], a[1:] // a inside will always be ["b", "c"]
}

Swapping looks nice

And reminds a lot the python way of swapping variables.

a, b := 1, 2
b, a = a, b

Methods can return multiple variables

This is very helpful while programming, a nice to have feature. The only thing that I miss, is the lack of a tuple, but the same result can be achieved creating a specific struct type

The not so good stuff

There's no syntax for while

All loops are for loops. I do understand the point of having a small syntax. And most of loops you'll write are regarding collections. But for all the places that a while would apply, it is still a bit awkward for me. Like consuming the values from a pile.

a := []string{"a", "b", "c"}
for len(a) > 0 {
el := a[0]
a = a[1:]
}

Or doing an infinite loop

for true {
fmt.Println("never ending")
}

Handling Errors

I do like the way Go deals with errors. But there's only one drawback, you'll see your code with lots of boilerplate. Just to interact with errors.

_, err := fetchSomething()if err != nil {
return err
}

Practical examples

One of the steps of my learning path is always regarding some practical challenges. I moved on and implement some standard problems that are already pretty known. For sure, I try to apply some nice routinely activities here such as testing and refactoring.

You can choose practical examples at your will. The random set of basic stuff I've picked for now is:

You can check those implementations here. Feel free to clone, fork, run the tests and play around if you want. Just be aware that it has the only purpose of learning.

I hope this can help you with learning choices and ideas. If you wanna share your thoughts, agreements and disagreements, feel free to be honest.

--

--