Free Go cheat sheet

Go cheat sheet

Go on one page: variables, functions, slices and maps, structs, the error pattern, and goroutines.

Variables and typesFunctionsSlices and mapsStructsControl flowErrors and goroutines

← All cheat sheets

Go Cheat Sheet

Quick reference from FreeCodingCourses.com

Variables and types

var x int = 5
explicit declaration.
y := 5
short form, type inferred. Only inside functions.
const pi = 3.14
a constant.
var s string var ok bool
zero values: "", false, 0.
var xs []int
a nil slice, ready to append to.

Functions

func add(a, b int) int { return a + b }
typed params and return.
func divide(a, b int) (int, error)
multiple returns; error is the Go convention.
q, err := divide(10, 2)
capture both values.
func sum(nums ...int) int
variadic: any number of ints.
defer file.Close()
run this when the function returns.

Slices and maps

xs := []int{1, 2, 3}
a slice (growable list).
xs = append(xs, 4)
append returns a new slice; reassign it.
len(xs) xs[1:3]
length and sub-slice.
m := map[string]int{"a": 1}
a map (key/value).
v, ok := m["a"]
ok is false if the key is missing.

Structs

type User struct { Name string Age int }
a struct groups fields.
u := User{Name: "Ada", Age: 36}
create one.
u.Name
access a field.
func (u User) Greet() string { ... }
a method on User.

Control flow

if x > 0 { ... } else { ... }
no parentheses, braces required.
for i := 0; i < 5; i++ { ... }
the only loop keyword in Go.
for i, v := range xs { ... }
range over a slice or map.
for cond { ... }
for with one condition acts like while.
switch x { case 1: ... default: ... }
no fallthrough by default.

Errors and goroutines

if err != nil { return err }
the error check you write constantly.
go doWork()
run a function concurrently in a goroutine.
ch := make(chan int)
a channel to pass values between goroutines.
ch <- 5 v := <-ch
send into and receive from a channel.

Unlock the full Go cheat sheet

You're seeing 2 of 6 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.

No spam, unsubscribe anytime. See our privacy policy.

Go questions, answered

Is it called Go or Golang?

The language is Go. Golang is the nickname that stuck because the website is golang.org and Go is hard to search for. They are the same thing; use golang as a search keyword and Go when you write about it.

What is a goroutine?

A goroutine is a function running concurrently, started by putting go in front of a call. They are very lightweight, so you can run thousands. Goroutines usually talk to each other through channels, which pass values safely between them.

Why does Go not have exceptions?

By design. Go treats errors as ordinary return values you check explicitly, which makes the failure paths visible in the code. It feels verbose at first, but it is a deliberate choice for clarity. panic and recover exist for truly exceptional cases, not routine errors.

Where to go next