An Intro to Go

Overview

  • What is Go?
  • Characteristics
  • Syntax and use-cases
  • The learning path

Don’t expect that Go will save the world, but I must say it’s a pleasure to work with!

What is Go?

Go is programming language born inside Google, created aiming software engineers and looking to solve problems Google faced at that time (conceived in 2007 and officially released in 2009). The main design goals for the language were:

  • Productivity
  • Multicore processors
  • Networking
  • Large codebases

If you’re gonna look for more resources about Go, you better look for Golang/golang. I consider the word “Go” not very searchable. As you may know, naming things is one of the hardest things in Computer Science.

Go creators (Robert Griesemer, Rob Pike and Ken Thompson) are a mix of academia and industry. They looked for performance and low-level details without neglecting ease of use and bootstrapping.

Characteristics

Among the many features the language has, those who catch my eyes are:

  • Static typing but a bit relaxed
  • Concurrency
  • Productivity
  • Web servers
  • Multi-platform binary
  • Great tools: test, coverage and performance
  • Code formatter
  • Great documentation
  • Amazing for CLI apps and APIs (my opinion of course!)

Syntax and use-cases

Go is in many aspects, similar to C, but also different of C. It favors brevity, simplicity and safety.

I consider very important to learn new paradigms and programming languages. To look at a problem and see how it can be solved with the proper technology is something quite interesting. There are a lot of specialized things, created to solve very specific problems and we must know that!

With Go I find it particularly very useful for distributed systems. The language was created with multicores in mind, so it’s a feature you already have when choosing it.

In 2017 a coworker was starting a new project at my previous company. It was a CI/CD container platform. One day he came to me and asked for an opinion: “What do you think of Go?” He’d started to write it in Python and was happy with the result, but he’d like to try something new. The first aspects I came to mention was - how the language is very readable and explicit, two things I judge essential for big codebases and complex software.

As we were using Docker and they provide, besides Python, a Go SDK, we decided to try it. Let’s see a bit of code to illustrate the syntax:

 1package main
 2
 3import (
 4	"fmt"
 5)
 6
 7func main() {
 8	hoursInADay := 24
 9	fmt.Printf("A day has %d hours", hoursInADay)
10}

Notice that, every Go code lives inside a package. A package is nothing more than a dir inside your project. If your code is executable, it necessarily must have a main function in the main package. This function works as the starting point of your application.

Every function call in Go is preceded by the package: fmt.Printf(...). fmt is the package and Printf(...) an exported function from this package. Functions beginning with an uppercase letter are is visible to other packages, while functions beginning with a lowercase letter are visible only inside its own package.

The support for tests is really nice too. No frameworks, libs or whatever. The language has the test tools built-in. Let’s see an example:

 1package main
 2
 3import "testing"
 4
 5func TestReverse(t *testing.T) {
 6	tests := []struct {
 7		in  string
 8		out string
 9	}{
10		{"apple", "elppa"},
11		{"banana", "ananab"},
12		{"mango", "ognam"},
13	}
14
15	for _, tt := range tests {
16		t.Run(tt.in, func(t *testing.T) {
17			str := Reverse(tt.in)
18			if str != tt.out {
19				t.Errorf("got %q, want %q", str, tt.out)
20			}
21		})
22	}
23}

This test validates if a string is reversed. As it’s a table test, we can pass multiple values of input and output and see if it works. Go also has a specific kind of test to validate performance, known as benchmark tests.

The learning path

As the language is concise and its syntax is kind of familiar for people who’ve learned programming languages or have been in a CS course, no big surprises.

I first started learning with A Tour of Go. It covers all the features and better yet, with lots of exercises to practice. After that, I read Introducing Go and today I use The Bible for reference. Both are great books and rest assured there are a lot of advanced books on the topic. I’ll talk about them in future posts.

The best way to try Go is the Go Playground. It’s a REPL (read, eval, print and loop) on the web! No installation, no headaches, just code.

It’s been almost 3 years since I started working and playing with Go and I must say that I really enjoy it.

Well, that’s it. I hope you enjoy this overview and in the next post, I’ll go deeper with code. Thanks for reading!


go

820 Words

2019-01-02 00:05 +0000