Basics

Installation

Follow the installation on the Go Docs for your operating system

Once done with that, you will need to add ~/go/bin to your PATH

Hello World

First, create a new directory for your project. In it create a module to enable dependency tracking - you can do this with:

go mod init example/hello

Make a new project directory with a file called main.go with the following content

Go has a main function that needs to be created for every package and it will be run automatically. We also need to import fmt to print content to the screen. Also note that Go strings must have double quotes "hello" or we will get an error

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

We can run this with the following command from the project dir

go run main.go

Or since it's a module, we can just do:

go run .

Build and Run

We can build binaries for an application with go install which can then be executed from the terminal

The simplest way to build a program is using go build which will build it and output it in the current directory

For example, we can build and run it as follows:

go build

./hello

You can also install the package globally with:

go install

Which will add this to ~/go/bin and can just be run using:

hello

Variables and Data Types

Go has the following data types

  • string
  • bool
  • int int8 int16 int32 int64
  • uint uint8 uint16 uint32 uint64
  • byte (uint8)
  • rune (int32)
  • float32 float64
  • complex64 complex128

There are a few different ways to create variables, note that if we create a variable and do not use it we will get an error

Var

var name string = "John"

Or with a separate declaration and initialization:

var age int

age = 5

Type Inference

var name = "John"

Get Type

We can make use of the following function to get the type of a variable

fmt.Printf("%T",name)

Constants

We can define constants with the const keyword

const name = "John"

Global Variables

We can declare global variables by defining them outside of the main function

Shorthand Method

Inside of a function we can declare variables using the assignment operator with the following without having to specify the type of var keyword

name := "John

Multiple Assignments

We can do multiple assignments as follows

name, age := "John", 15

Booleans

Booleans in Go a represented using a bool type and are either true or false

var online = true

var active bool

Booleans are implicily initialized as false

The boolean operators are:

  • && - and
  • || - or
  • ! - not

Numbers

Go has basic numeric types for representing integers and floating point values.

Some of these are int, float64, and uint and conversion between types can be done using functions with the name of the respective type. For example:

var x int = 5
var y = float64()

Go supports the normal numerical operations such as +, -, *, /, and %. Note that for integer division the number is truncated back to an int

Numeric operations are only supported between numbers of the same type

Strings

string is an immutable sequence of bytes and can be defined using double quotes:

var str1 = "Hello"
str2 := "World"

Concatenation of strings can be done using + like so:

str3 := str1 + " " + str2

The strings package also has many methods for working with strings:

import "strings"

func caps(name string) {
    strings.ToUpper(name)
}

String Formatting

String formatting can be done using the fmt package which can format strings using fmt.Sprintf like so:

str := fmt.Sprintf("int %d, int %03d, float %.3f, string %s, props %+v", 5, 7, 1.5, "hello", myStruct)
// str == "int 5, int 007, float 1.500, string hello, props { Name "hello" }"

Lots of other formatting options also exist for string formatting and can be found in the docs

Packages

Importing Packages

We can import multiple packages by declaring each package in the import statement on a different line

import (
    "fmt"
    "math"
)

Dependencies

We can add dependencies to our Go module by simply importing them in the code that needs it, for example:

import "rsc.io/quote"

And then Go can automatically add it to our go.mod with:

go mod tidy

Creating Packages

Create a new folder and in it you can define the package name and some functions in it

All files within the folder should have the same package name, for example the file mypackage/mypackage.go would look like so:

package mypackage

func myfunction() {
    ...
}

We then import the package by referring to its folder path in the import function

Functions

Functions are defined with the func keyword, the function name, inputs, and return types and values

func greet(name string) string {
    return "Hello " + name
}

The convention is to start functions with an uppercase letter if they're public and lowercase if they're private

Functions can also have multiple input parameters. If these are of the same type the type can just be specified once:

go canMessage(visible, online bool) bool {
    return visible && online
}

Variadic Functions

A variadic function is a function that takes a variable number of arguments, this is done using ... in the type of the function:

func formatMany(format string, strs ...string) string {
	result := ""

	for _, str := range strs {
		result += " " + fmt.Sprintf(format, str)
	}

	return result
}

func main() {
	str := formatMany("Hello %s", "John", "Alice", "Bob")
	fmt.Println(str)
}

The variadic parameter must be the last in the parameter list

A slice can also be passed into a variadic function using ... after the name of the variable:

func main() {
	names := []string{"John", "Alice", "Bob"}
	str := formatMany("Hello %s", names...)
}

Arrays and Slices

In Go arrays are fixed length, and a slice is an array without a fixed size

Arrays

Arrays are fixed in size and can be defined with

var myArray [3]string

myArray[0] = "Hello"
myArray[2] = "Word"

Or with initial values

myArr := [2]string{"Hello", "World"}

Slices

A slice is essentially an array that does not have a fixed size

mySlice := []string{"Hello", "World","!"}

We can also make slices by using the same notation as other languages

newSlice := mySlice[2:5]

Elements can be added to a slice using the append function:

mySlice := []int{1,2,3}
mySlice = append(mySlice, 4,5,6)

Note that append is not a pure function and the original slice may be modified

Conditionals

Conditionals do not require parenthesis, however they can be used

If Else

if x < y {
    ...
} else if x == y {
    ...
} else {
    ...
}

Swtich/Case

switch x {
    case: 5:
        ...
    case 10:
        ...
    default:
        ...
}

Loops

There are two methods for building for loops

i := 1
for i <= 01 {
    ...
    i++
}
for i := 1; i <= 10; i++ {
    ...
}

Maps

Maps are key-value pairs and can be defined and accessed with

ages := make(map[string]int)

ages["Bob"] = 35
ages["John"] = 5

We can delete a value from a map with the delete function

delete(emails, "Bob")

We can also declare a map with initial values

emails := map[string]int{"Bob":35, "John":5}

Range

Range is used for looping through values

ids := []int{1,2,3}

for i, id := range ids{
    ...
}

If we are not using the i variable, we can use an _ to receive any inputs that we will not use

for _, id := range ids{
    ...
}
emails := map[string]int{"Bob":35, "John":5}

for k, v := range emails {
    ...
}

Pointers

A pointer allows us to point to the memory address of a specific value, we can get the pointer for a value with the & sign

a := 5
b := &a

If we wan to get back from the pointer to the actual value we can do that with the * operator

a == *b // true
a == *&a //true

We can modify the value of a pointer

*b = 10
a == 10 // true

The reason to use pointers can be more efficient when passing values

Closures

We can define anonymous functions to declare anonymous functions that can be used as closures

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


func main() {
    sum := adder()
    for i:= 0; i < 10; i++ {
        fmt.Println(sum(i)) // 0 1 3 6 ...
    }
}

Structs

Structs are like classes

Structs can contain values and functions, of which we can have value reveivers and pointer receivers. Value receivers just do calculations, Pointer Receivers modify data

type Person struct {
    firstName string
    lastName string
    age int
}

Structs can be instantiated using all the property names or using the values provided in the struct order:

person1 := Person{firstName: "John", lastName: "Smith", age: 25}

person2 := Person{"John", "Smith", 25}

new struct

In Go we can also create an instance of a struct where each value has the default value using the new function which returns a pointer to the new struct

person := new(Person)

Struct Initialization

There are a few different ways to initialize structs,

  • var Type/Type{} initializes a struct with all default values
  • new initializes a struct to all default values and returns a pointer to it
    • Practical for generic functions where we can't actually do &T{}
  • make initilizes a slice, map, or channel
// basically the same
var user1 User
user2 := User{}

user3 := new(User)       // *User
user4 := make([]User, 4) // []User

Methods

These are functions that can be called on a struct directly using a Receiver argument that is defined after the func keyword:

// receives a copy of the input
func (p Person) myValueReceiver() string {
    return "Hello " + p.firstName
}

// received the original input, can mutate values of the original struct
func (p *Person) myPointerReceiver() {
    p.age++
}

They can then be called using the syntax as seen below

func main() {
    person := Person{firstName: "John", lastName: "Smith", age: 25}
    person2 := Person{"John", "Smith", 25}

    person.firstName // John

    person.myValueReceiver() // Hello John
    person.myPointerReceiver() // person.age = 26
}

Interfaces

type Shape interface {
    area() float64
}

type Circle struct {
    x, y, radius float64
}

type Rectangle struct {
    width, height float64
}

func (c Circle) area() float64 {
    return math.Pi * c.radius * c.radius
}

func (r Rectangle) area() float64 {
    return r.width * r.height
}

func getArea(s Shape) float64 {
    return s.area()
}

Web

To work with HTTP requests we can use thenet/http package which allows us to define a function to handle a specific route

package main

import ("fmt"
        "net/http"
)

func main(){
    http.HandleFunc("/", index)
    fmt.Println("Server Starting")
    http.ListenAndServe(":3000", nil)
}

func index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello World")
}

Randomness

The math/rand package offers some utilities for generaating random numbers

Numbers generated by math/rand are not truly random and can be seeded before retreiving random numbers, like so:

rand.Seed(time.Now().UnixNano())

// random int between 0 and 100
num := rand.IntN(100)

Multiple Return Values

Functions in go can return multiple values:

func user(message string) (int, string) {
	return 1, "Bob"
}

These functions can be used by assigning multiple variables when calling, like:

func main() {
    // declares 2 variables
	id, name1 := user()

	if id < 0 {
		panic("bad user")
	}

    // can use := as long as you are declaring at least 1 new variable
    // e.g. name2 in this case
	id, name2 := user()

	log.Printf("%v %v %v", id, name1, name2)
}

This is typically used with functions that can return errors as can be seen below

Typical Error Flow

Functions that error will typically return an error alongside the value as a multi return, this would be used like:

func app() string {
    name, err := getUsername()

    if err != nil {
        // we had an error
        return panic("User not found")
    }

    // did not have an error
    log.Print(name)
}

If the err value is not nil it indicates that there was an error and the result of the function cannot be trusted to be correct

As discussed above, the typical flow for multiple errors then looks like so:

func main() {
	s, err := doThing1()
	if err != nil {
		panic("Got an error 1")
	}

	i, err := doThing2()

    if err != nil {
		panic("Got an error 2")
	}

	doThing3(s, i)
}

Note that we also don't need to pick a different name for each err

Logging

Nothing much, just nice to know that the log module exists and makes logging easier than fmt.Println if you're just debugging or adding normal logs. fmt is for formatting:

log.Print("message: ", mymsg)
log.Print("object: ", myobj)

JSON

Go has a builtin JSON library, it's pretty straightforward to use. It consists of defining the JSON properties of the struct you'd like to parse and then parsing it using a decoder

For example:

import (
	"encoding/json"
	"fmt"
	"strings"
)

type MyData struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {
	data := strings.NewReader("{\"name\": \"Bob\", \"age\": 25 }")

    // decoder needs an io.Reader
	decoder := json.NewDecoder(data)

	var output MyData
	err := decoder.Decode(&output)

	if err != nil {
		panic(err)
	}

	fmt.Printf("Decoded JSON: %+v", output)
	// Decoded JSON: {Name:Bob Age:25}
}

Defer Functions

The defer keyword allows us to define something that will be executed after the parent function is returned, for example:

func atEnd(message string) {
	log.Print("end: ", message)
}

func main() {
	defer atEnd("Main done")

	log.Print("Starting main")
}

The function can also be defined internally like so:

func atEnd(message string) {
	log.Print("end: ", message)
}

func main() {
	defer atEnd("Main done")
	defer func() {
		log.Print("Just before the other defer")
	}()

	log.Print("Starting main")
}