Go Harvest — Go

Tally Kang Dédé's orange harvest with real Go: variables, strings, slices, maps, errors and defer. 16 levels, right in the browser.

What you learn in this game

In Go every variable has a fixed type and must be made before it is used, with var name type or name := value. A variable made without a value is not empty: it holds its type's zero value. At Kang Dédé's orange shed every program is run by real Go 1.27, and the chalkboard copies exactly what it prints.

Go's operators follow the numbers' type. +, -, * and / on two ints give an int, and % gives the remainder of a division. For a fraction the numbers must be float64, and Go only changes a type when you ask it to.

Letters like é do not fit in one byte. Go stores a string as UTF-8 bytes, and calls one Unicode letter a rune. Indexing and len on a string work in bytes; range over a string works in runes.

Go has only one word for loops: for. It can take three parts as in C, a condition alone, or walk a slice, a string or a map with range. range over a slice gives two values each step: the index and a copy of what is there.

A switch in Go checks its cases from the top and runs one, the first that matches. A switch with no value works like a chain of ifs. Every case ends in its own block.

A slice holds three things: a pointer to an array, a length (len) and a capacity (cap). Slicing a slice copies nothing. append writes into the same array when the capacity is enough, and makes a new array when it is not.