Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
title permalink slug date tags excerpt layout
Go Tour Summary
programming, go
These are my summary notes of 'A Tour of Go' - which is meant for people who are familiar with programming to have a quick tour

These are my summary notes of A Tour of Go - which is meant for people who are familiar with programming to have a quick tour of the Go language. I'm most familiar with dynamic languages (R, Python, JavaScript) so there are some go-lang features that naturally feel new by the nature of Go being a statically typed language. A subsequent post will expand upon some of the more advanced features like pointers, concurrency and polymorphism.

Named Return Values

A return statement without arguments returns the named return values. This is known as a "naked" return.

func split(mynumber int) (x, y int) {
	x = mynumber * 4
	y = mynumber - x

func main() {
// This will print x and y:
// 68 -51


The var statement declares a list of variables; as in function argument lists, the type is last.

Variables with initializers

A var declaration can include initializers, one per variable.

If an initializer is present, the type can be omitted; the variable will take the type of the initializer.

var a int // will have a value of 0 as a placeholder
var f float64 // 0
var b bool // false
var s string // ""

var i, j int = 1, 2 // explicit

var k, l = 1, 2 // implicit


Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

func main() {
  // := only within fn
  k := 3

Types in go



int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128

Type conversions

var i int = 42
var f float64 = float64(i)

i := 42
f := float64(i)


Constants are declared like variables, but with the const keyword.

const Pi = 3.14


Go has only one looping construct, the for loop.

func main() {
	sum := 0
	for i := 0; i < 10; i++ {
		sum += i

The init and post statements are optional.

sum := 1
for ; sum < 1000; {
  sum += sum


Go's if statements are like its for loops; the expression need not be surrounded by parentheses ( ) but the braces { } are required.

If with a short statement

Like the for loop, the if statement can start with a short statement to execute before the condition.

Variables declared by the statement are only in scope until the end of the if.

func pow(x, n, lim float64) float64 {
  // v doesn't exist outside of the if statement scope
  if v := math.Pow(x, n); v < lim {
		return v
	return lim


A switch statement is a shorter way to write a sequence of if else statements. It runs the first case whose value is equal to the condition expression.

Go's switch is like the one in C, C++, Java, JavaScript and PHP, except that Go only runs the selected case, not all the cases that follow. In effect, the break statement that is needed at the end of each case in those languages is provided automatically in Go. Another important difference is that Go's switch cases need not be constants, and the values involved need not be integers.

func main() {
	switch 1 {
	case 1:
	case 1 + 1:
		fmt.Println("doesnt match")
// matches

Switch with no condition

Switch without a condition is the same as switch true. This construct can be a clean way to write long if-then-else chains.

func main() {
	t := time.Now()
	switch {
	case t.Hour() < 12:
		fmt.Println("Good morning!")
	case t.Hour() < 17:
		fmt.Println("Good afternoon.")
		fmt.Println("Good evening.")


Defer evaluates but doesn't execute

func main() {
	defer fmt.Println("world")
// hello1
// hello2
// world

Stacking defers

Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.

func main() {
	for i := 0; i < 4; i++ {
		defer fmt.Println(i)
// counting
// done
// 3
// 2
// 1
// 0


Pointers are useful if you have read something into memory and you don't want to duplicate it. So you can just point to the memory address. Useful for optimising your code.

So if you want to change a variable stored in memory, you can 'mutate' it by using pointers.

The type *T is a pointer to a T value. Its zero value is nil.

var p *int

The & operator generates a pointer to its operand. n.b. In 3+6, 3 and 6 are operands. And the + is the operator.

i := 42
p := &i // a pointer p=0xc00002c008
z := p // 'dereferences the pointer. z=42
fmt.Println("i",i,"p",p, "z", z)
// i 42 p 0xc00002c008 z 42

The * operator denotes the pointer's underlying value.

fmt.Println(*p) // read i through the pointer p
// 42
*p = 21         // set i through the pointer p
// 21

This is known as "dereferencing" or "indirecting".


A struct is a collection of fields.

type Vertex struct {
	X int
	Y int

func main() {
	fmt.Println(Vertex{1, 2})
// {1 2}

Pointers to structs

Struct fields can be accessed through a struct pointer.

type Vertex struct {
	X int
	Y int

func main() {
	v := Vertex{1, 2}
	p := &v
	p.X = 10
// {10 2}

Struct Literals

A struct literal denotes a newly allocated struct value by listing the values of its fields.

You can list just a subset of fields by using the Name: syntax. The order of named fields is irrelevant.

The special prefix & returns a pointer to the struct value.

type Vertex struct {
	X, Y int

var (
	v1 = Vertex{1, 2}  // has type Vertex
	v2 = Vertex{X: 1}  // Y:0 is implicit
	v3 = Vertex{}      // X:0 and Y:0
	p  = &Vertex{1, 2} // has type *Vertex

func main() {
	fmt.Println(v1, p, v2, v3)
// {1 2} &{1 2} {1 0} {0 0}


The type [n]T is an array of n values of type T. The expression var a [10]int declares a variable a as an array of ten integers. An array's length is part of its type, so arrays cannot be resized.

func main() {
	var a [2]string
	a[0] = "Hello"
	a[1] = "World"
	fmt.Println(a[0], a[1])

	primes := [6]int{2, 3, 5, 7, 11, 13}
// Hello World
// [Hello World]
// [2 3 5 7 11 13]


An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array

func main() {
	primes := [6]int{2, 3, 5, 7, 11, 13}

	var sliced []int = primes[1:4]

A slice does not store any data, it just describes a section of an underlying array. Changing the elements of a slice modifies the corresponding elements of its underlying array. Other slices that share the same underlying array will see those changes.

func main() {
	names := [4]string{

	a := names[0:2]
	b := names[1:3]
	fmt.Println(a, b)

	b[0] = "XXX"
	fmt.Println(a, b)
// [John Paul] [Paul George]
// [John XXX] [XXX George]
// [John XXX George Ringo]

Slice literals

A slice literal is like an array literal without the length. This is an array literal: [3]bool{true, true, false}

And this creates the same array as above, then builds a slice that references it: []bool{true, true, false}

	q := []int{2, 3, 5, 7, 11, 13}
  // [2 3 5 7 11 13]

Slice length and capacity

The length of a slice is the number of elements it contains. The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice. The length and capacity of a slice s can be obtained using the expressions len(s) and cap(s).

Nil slices

The zero value of a slice is nil.

func main() {
	var s []int
	fmt.Println(s, len(s), cap(s))
	if s == nil {
// [] 0 0
// nil!

Creating a slice with make

Slices can be created with the built-in make function; this is how you create dynamically-sized arrays. The make function allocates a zeroed array and returns a slice that refers to that array:

a := make([]int, 5)  // len(a)=5

// To specify a capacity, pass a third argument to make:

b := make([]int, 0, 5) // len(b)=0, cap(b)=5

Appending to a slice

func main() {
	var s []int

	// append works on nil slices.
	s = append(s, 0)

	// The slice grows as needed.
	s = append(s, 1)

	// We can add more than one element at a time.
	s = append(s, 2, 3, 4)

func printSlice(s []int) {
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
// len=0 cap=0 []
// len=1 cap=1 [0]
// len=2 cap=2 [0 1]
// len=5 cap=6 [0 1 2 3 4]


The range form of the for loop iterates over a slice or map. When ranging over a slice, two values are returned for each iteration. The first is the index, and the second is a copy of the element at that index.

n.b. verbs in string templates.

var pow = []int{1, 2, 4, 8, 16}

func main() {
	for i, v := range pow {
		fmt.Printf("2**%d = %d\n", i, v)
// 2**0 = 1
// 2**1 = 2
// 2**2 = 4
// 2**3 = 8
// 2**4 = 16

You can skip the index or value by assigning to _.

for i, _ := range pow
for _, value := range pow

Map literals

Map literals are like struct literals, but the keys are required.

type Vertex struct {
	Lat, Long float64

var m = map[string]Vertex{
	"Bell Labs": Vertex{
		40.68433, -74.39967,
	"Google": Vertex{
		37.42202, -122.08408,

// m can also be expressed
var m = map[string]Vertex{
	"Bell Labs": {40.68433, -74.39967},
	"Google":    {37.42202, -122.08408},

func main() {
// map[Bell Labs:{40.68433 -74.39967} Google:{37.42202 -122.08408}]

Mutating Maps

// Insert or update an element in map m:
m[key] = elem

// Retrieve an element:
elem = m[key]

// Delete an element:
delete(m, key)

// Test that a key is present with a two-value assignment:
elem, ok = m[key]

Function values

Functions are values too. They can be passed around just like other values. Function values may be used as function arguments and return values.

Function closures

Go functions may be closures. A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense the function is "bound" to the variables.

func adder() func(int) int {
	sum := 0
	return func(x int) int {
		sum += x
		return sum

func main() {
	pos, neg := adder(), adder()
	for i := 0; i < 10; i++ {


Go does not have classes. However, you can define methods on types. A method is a function with a special receiver argument. The receiver appears in its own argument list between the func keyword and the method name. In this example, the Abs method has a receiver of type Vertex named v.

type Vertex struct {
	X, Y float64

// as a method
func (v Vertex) Abs() float64 {
	return math.Sqrt(v.X*v.X + v.Y*v.Y)

func main() {
	v := Vertex{3, 4}

// as a normal function
func Abs(v Vertex) float64 {
	return math.Sqrt(v.X*v.X + v.Y*v.Y)

func main() {
	v := Vertex{3, 4}

Pointer receivers

Methods with pointer receivers can modify the value to which the receiver points (as Scale does here 👇). Since methods often need to modify their receiver, pointer receivers are more common than value receivers.

With a value receiver, the Scale method operates on a copy of the original Vertex value. (This is the same behavior as for any other function argument.) The Scale method must have a pointer receiver to change the Vertex value declared in the main function.

type Vertex struct {
	X, Y float64

func (v Vertex) Abs() float64 {
	return math.Sqrt(v.X*v.X + v.Y*v.Y)

// method on a pointer
func (v *Vertex) Scale(f float64) {
	v.X = v.X * f
	v.Y = v.Y * f

func main() {
	v := Vertex{3, 4}
// {30 40}

// method on value receiver
func (v Vertex) Scale(f float64) {
	v.X = v.X * f
	v.Y = v.Y * f

func main() {
	v := Vertex{3, 4}
// {3 4}
// the result {3 4} is because the the original values are not changed. A copy is made on execution. As opposed to the pointer receiver, which modifies the original values.

Methods and pointer indirection (2)

Functions that take a value argument must take a value of that specific type:

var v Vertex
fmt.Println(AbsFunc(v))  // OK
fmt.Println(AbsFunc(&v)) // Compile error!

Methods with value receivers take either a value or a pointer as the receiver when they are called:

var v Vertex
fmt.Println(v.Abs()) // OK
p := &v
fmt.Println(p.Abs()) // OK


An interface type is defined as a set of method signatures. A value of interface type can hold any value that implements those methods.

A type implements an interface by implementing its methods. There is no explicit declaration of intent, no "implements" keyword

Interface values

Under the hood, interface values can be thought of as a tuple of a value and a concrete type: (value, type) An interface value holds a value of a specific underlying concrete type.

Interface values with nil underlying values

If the concrete value inside the interface itself is nil, the method will be called with a nil receiver. In some languages this would trigger a null pointer exception, but in Go it is common to write methods that gracefully handle being called with a nil receiver.

The empty interface

The interface type that specifies zero methods is known as the empty interface: interface{} An empty interface may hold values of any type. Every type implements at least zero methods. Empty interfaces are used by code that handles values of unknown type. For example, fmt.Print takes any number of arguments of type interface{}.

Type assertions

A type assertion provides access to an interface value's underlying concrete value. t := i.(T) This statement asserts that the interface value i holds the concrete type T and assigns the underlying T value to the variable t. If i does not hold a T, the statement will trigger a panic. To test whether an interface value holds a specific type, a type assertion can return two values: the underlying value and a boolean value that reports whether the assertion succeeded. t, ok := i.(T)

func main() {
	var i interface{} = "hello" // implicit string type

	s := i.(string)
	fmt.Println(s) //"hello"

	s, ok := i.(string)
	fmt.Println(s, ok) //"hello", true

	f, ok := i.(float64)
	fmt.Println(f, ok) //0, false

	f = i.(float64) // panic
	fmt.Println(f) //panic: interface conversion: interface {} is string, not float64

Type switches

A type switch is a construct that permits several type assertions in series. A type switch is like a regular switch statement, but the cases in a type switch specify types (not values), and those values are compared against the type of the value held by the given interface value.

func do(i interface{}) {
	switch v := i.(type) {
	case int:
		fmt.Printf("Twice %v is %v\n", v, v*2)
	case string:
		fmt.Printf("%q is %v bytes long\n", v, len(v))
		fmt.Printf("I don't know about type %T!\n", v)

func main() {
// Twice 21 is 42
// "hello" is 5 bytes long
// I don't know about type bool!


Go programs express error state with error values. The error type is a built-in interface similar to fmt.Stringer:

type error interface {
    Error() string

As with fmt.Stringer, the fmt package looks for the error interface when printing values. Functions often return an error value, and calling code should handle errors by testing whether the error equals nil.

i, err := strconv.Atoi("42")
if err != nil {
    fmt.Printf("couldn't convert number: %v\n", err)
fmt.Println("Converted integer:", i)


A goroutine is a lightweight thread managed by the Go runtime.

func say(s string) {
	for i := 0; i < 3; i++ {
		time.Sleep(100 * time.Millisecond)

func main() {
	go say("world")
// hello
// world
// world
// hello
// hello


🚧 TODO - continue on here



  • unused variables are not allowed by the compiler - very cool

  • hard concepts (read more on)

    • pointer indirection
      • performance
      • modify in place
      • when optional, pointer to a string... no empty strings in go. But the pointer to the string will be nil
    • interfaces and polymorphism (types)
    • concurrency
    • stringer, but why? Pretty printing?
  • skipped

    • exercises
    • reader
    • images
    • routines, channels, buffered channels, range and close, select,sync.Mutex
  • typing a particular concept into youtube usual has a human to explain it.

    • sentdex is pretty good at explaining in 5mins
  • is go functional? Nope and it's not OO. It's procedural. Very explicit.