Learning Go

Jon Bodner

O'Reilly Media, Inc.

Go remains a small language with a small feature set. It still lacks inheritance, aspect-oriented programming, function overloading, operator overloading, pattern matching, named parameters, exceptions, and many additional features that complicate other languages.

Well that sounds good, I think complexity is the worst part of software engineering.

Go has a very simple syntax and includes a formatter for consistency.

package main

import "fmt"

func main() {
	fmt.Println("Hello, world!")
}

Go has types like other languages, but single quotes and double quotes are not interchangeable. A single quoted string is called a rune. It also supports raw strings with backquotes (`) if you need whitespaces or raw unformatted text. Strings in Go are also immutable.

var letter rune = 'X'

For simplicity, Go doesn’t allow automatic type promotion between variables. This means you need to make the types match before you can compare them:

var x int = 10
var y float64 = 30.2

var result int = x + int(y)

There is also a short-hand to declare variables:

var x int = 10
x := 10     // same thing

Like JavaScript, you can use const to make values immutable.

The language also has some conventions to make it easier to use in large teams, such as not allowing unused variables.

Declaring arrays seems a bit strange, and it only supports one dimensional arrays:

var x = [3]int{10, 20, 30}

Go also considers the size of the array to be its “type”, so this array has a type of [3]int and you wouldn’t be able to compare it to an array with [4]int. The author says what you really want to use instead are called “slices”.

var x = []int{10, 20, 30}

Slices can be appended to each other:

var y = []int{20, 30, 40}
x = append(x, y...)

That also seems similar to the JavaScript convention of merging arrays with a spread operator:

let x = [1, 2, 3]
let y = [...x, 4]

I don’t like this arrays vs. slices syntax or the short-hand for declaring variables… it’s already making the language seem complex for one that is designed to be simple.

The language also includes maps, and another concept called struct which is useful for message passing?

type firstPerson struct {
    name string
    age  int
}

Golang has the usual types of control flow programming as other languages, such as if else and switch statements, but Go also includes a goto statement, which is interesting… considering it’s practically forbidden in modern programming.

I’m just going to stop here because Go isn’t as simple or elegant as I expected, so maybe it’s not the best book, or the right language to learn at the moment.