t0ul

The Go Beginner's Cheat Sheet

Updated November 2026 for Go 1.27. Every snippet below was run, and the output in the comments is what it printed. The 2022 version had two mistakes, which are fixed here: a switch doesn’t need a default, and printing a pointer to a struct shows an &.

When I first learned Go I kept looking up the same handful of things, mostly because they work differently from Python: printing, converting between types, and loops that don’t have a while.

So here are the things I looked up most, in one page. To try any of them, paste it into the main function of a file, add the imports it uses, and go run . it.

1. if and switch

if, and if with a short statement

There’s no elif. You write else if. You can also declare a variable right in the if, and it only exists inside it:

if n := 7; n < 0 {
	fmt.Println(n, "is negative")
} else if n < 10 {
	fmt.Println(n, "has one digit")
} else {
	fmt.Println(n, "has more digits")
}
// 7 has one digit

switch

A switch doesn’t need a default, and it doesn’t fall through to the next case the way C does, so there’s no break to forget. One case can match several values:

day := "sat"
switch day {
case "sat", "sun":
	fmt.Println("weekend")
case "mon":
	fmt.Println("ugh")
}
// weekend

2. Loops

Go has one loop keyword, for, and it covers everything.

// the classic
for i := 0; i < 3; i++ {
	fmt.Print(i, " ")
}
// 0 1 2

// counting, Go 1.22 and later
for i := range 3 {
	fmt.Print(i, " ")
}
// 0 1 2

// over a slice: index and value
langs := []string{"go", "python", "rust"}
for i, l := range langs {
	fmt.Println(i, l)
}
// 0 go
// 1 python
// 2 rust

There’s no while keyword. A for with only a condition is a while loop, and a for with nothing is a loop forever:

n := 0
for n < 3 { // while n < 3
	n++
}
fmt.Println("n is", n)
// n is 3

for { // loop forever, until a break or return
	break
}

3. Converting between types

There’s no int() or str() like Python. Conversions between numbers and text go through the strconv package, and the ones that can fail give you an error back.

// bytes and strings
s := string([]byte{72, 105})
fmt.Println(s) // Hi
b := []byte("Hi")
fmt.Println(b) // [72 105]

// string to int: check the error
age, err := strconv.Atoi("42")
fmt.Println(age+1, err) // 43 <nil>
_, err = strconv.Atoi("forty-two")
fmt.Println(err) // strconv.Atoi: parsing "forty-two": invalid syntax

// int to string
fmt.Println(strconv.Itoa(42) + "!") // 42!

// bools and floats
ok, _ := strconv.ParseBool("true")
fmt.Println(ok) // true
f, _ := strconv.ParseFloat("3.14", 64)
fmt.Println(f * 2) // 6.28
fmt.Println(strconv.FormatBool(true), fmt.Sprint(3.5)) // true 3.5

One that trips everyone up: string(65) does not give you "65". It gives you "A", the character with that code, and go vet warns you about it. Use strconv.Itoa for numbers.

4. Printing structs

fmt can print any struct. The verb decides how much you see:

type employee struct {
	name   string
	age    int
	salary int
}

emp := &employee{name: "Toul", age: 24, salary: 500000} // a writer can dream

fmt.Printf("%v\n", *emp)  // {Toul 24 500000}
fmt.Printf("%v\n", emp)   // &{Toul 24 500000}
fmt.Printf("%+v\n", *emp) // {name:Toul age:24 salary:500000}
fmt.Printf("%#v\n", *emp) // main.employee{name:"Toul", age:24, salary:500000}
fmt.Printf("%T\n", emp)   // *main.employee

%+v is the one I use most when debugging, because it shows the field names. %T tells you the type, which is handy when you’re not sure what you’re holding.

5. Slices

A slice is Go’s list. The slices package has the helpers you’d expect, and min and max are built in since Go 1.21:

nums := []int{3, 1, 2}
nums = append(nums, 5)
slices.Sort(nums)
fmt.Println(nums, slices.Contains(nums, 5), len(nums)) // [1 2 3 5] true 4
fmt.Println(nums[1:3])                                  // [2 3]
fmt.Println(min(4, 2, 9), max(4, 2, 9))                 // 2 9

6. Maps

A map is Go’s dictionary. Reading a key that isn’t there gives you the zero value, so use the two-value form when “missing” and “zero” mean different things:

ages := map[string]int{"toul": 24, "ada": 36}
ages["linus"] = 55

if a, found := ages["ada"]; found {
	fmt.Println("ada is", a)
}
// ada is 36

delete(ages, "linus")
fmt.Println(slices.Sorted(maps.Keys(ages))) // [ada toul]

Maps have no order, so if you print or loop over the keys and need them in order, sort them first, like the last line does.

7. Errors

Go doesn’t have exceptions. Functions that can fail return an error as their last value, and you check it:

_, err := os.Open("missing.txt")
if errors.Is(err, fs.ErrNotExist) {
	fmt.Println("no such file")
}
// no such file

wrapped := fmt.Errorf("loading config: %w", err)
fmt.Println(errors.Is(wrapped, fs.ErrNotExist)) // true

Wrapping with %w adds context to the message and keeps the original error inside, so errors.Is can still find it.

Where to go next

If you want to go from looking things up to building small programs that do real work, my free book Automate the Boring Stuff with Go starts from the terminal and builds up one small program at a time. And if you’re picking libraries, here are the Go frameworks I’d use in 2026.

← Back to the blog