Golang Quick Reference and Tips

(Last edited: October 29, 2019)

Basic Types

bool

string

int // usually either 32 for 32-bit systems or 64 for 64-bit systems
int8  int16  int32  int64

uint // usually either 32 for 32-bit systems or 64 for 64-bit systems
uint8 uint16 uint32 uint64
uintptr // usually either 32 for 32-bit systems or 64 for 64-bit systems

byte // alias for uint8

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

float32 float64

complex64 complex128 // real and imaginary components

Zero Values

  • 0 for numeric types
  • false for the boolean type
  • "" (the empty string) for strings

Pass by value

When a pointer is passed to a function, it is still passed by value in the sense that a copy of the value of the pointer is created. While assigning new values to the value that the pointer is pointing to mutates the value, assigning a new address to the pointer won’t change the pointer value.

Arrays

Arrays are value types. This means that when they are assigned to a new variable, a copy of the original array is assigned to the new variable. If changes are made to the new variable, it will not be reflected in the original array.

Slices

Slices are reference types. When a slice is assigned to a new variable, they both point to the same internal data structure. Hence changes made in one will reflect in the other.

Length and Capacity of a Slice

The length of the slice is the number of elements in the slice.

The capacity of the slice is the number of elements in the underlying array starting from the index from which the slice is created.

Once the capacity is met, appending to a slice will return a new slice with a larger capacity and a new pointer address. It’s evident in the below example:

package main

import (
    "fmt"
)

func change(s ...string) {
    s[0] = "Go"
    s = append(s, "playground")
    fmt.Println(s)
}

func main() {
    welcome := []string{"hello", "world"}
    change(welcome...)
    fmt.Println(welcome)
}

Maps

Maps are reference types. When a map is assigned to a new variable, they both point to the same internal data structure. Hence changes made in one will reflect in the other.

Pointers

Do not pass a pointer to an array as a argument to a function. Use slice instead.

Structs

  • Structs are value types and are comparable if each of their fields are comparable. Two struct variables are considered equal if their corresponding fields are equal.
  • Struct variables are not comparable if they contain fields which are not comparable

Methods

When to use pointer receiver and when to use value receiver.

Generally, pointer receivers can be used when changes made to the receiver inside the method should be visible to the caller.

Pointers receivers can also be used in places where it’s expensive to copy a data structure. Consider a struct that has many fields. Using this struct as a value receiver in a method will need the entire struct to be copied which will be expensive. In this case, if a pointer receiver is used, the struct will not be copied and only a pointer to it will be used in the method.

In all other situations, value receivers can be used.

Value receivers in methods vs Value arguments in functions

When a function has a value argument, it will accept only a value argument.

When a method has a value receiver, it will accept both pointer and value receivers.

Pointer receivers in methods vs Pointer arguments in functions

Similar to value arguments, functions with pointer arguments will accept only pointers whereas methods with pointer receivers will accept both pointer and value receiver.