This Go quick reference covers the core syntax and programming patterns students need when writing small to medium programs in Golang. It helps students remember how to declare variables, define functions, use loops, build data structures, and handle errors. A cheat sheet is useful because Go is strict about types, formatting, and unused code, so small syntax details matter.
Students in grades 11-12 can use it while practicing algorithms, command-line programs, and basic concurrent programming.
Key Facts
- A Go program starts with package main and runs from func main() { ... } when it is built as an executable.
- Declare variables with var name type = value or use short declaration inside functions with name := value.
- A function is defined with func name(parameter type) returnType { ... }, such as func add(a int, b int) int { return a + b }.
- Use if condition { ... } else { ... } without parentheses around the condition, but braces are required.
- A for loop can act as a counting loop with for i := 0; i < 10; i++ { ... } or as a while-style loop with for condition { ... }.
- Slices are flexible views over arrays and can be created with values := []int{1, 2, 3} or extended with values = append(values, 4).
- Maps store key-value pairs and are created with scores := map[string]int{"Ana": 95}, then read with value, ok := scores["Ana"].
- Go handles many failures by returning an error value, so code should check if err != nil before using the result.
Vocabulary
- Package
- A package is a group of Go files that share the same package name and can organize reusable code.
- Goroutine
- A goroutine is a lightweight concurrent task started with the keyword go before a function call.
- Slice
- A slice is a resizable sequence that refers to part or all of an underlying array.
- Map
- A map is a collection that stores values by unique keys, such as names mapped to scores.
- Struct
- A struct is a custom type that groups related fields into one value.
- Interface
- An interface is a type that describes required method behavior without storing the actual implementation.
Common Mistakes to Avoid
- Using := outside a function is wrong because short variable declarations are only allowed inside function bodies.
- Forgetting to check err is risky because Go functions often return a value and an error, and the value may not be safe to use when err != nil.
- Writing unused variables or imports causes a compile error because Go requires code to be clean and intentional.
- Confusing arrays and slices leads to bugs because arrays have fixed length, while slices can grow with append.
- Starting a goroutine and ending main immediately is wrong because the program can exit before the goroutine has time to finish.
Practice Questions
- 1 Write a Go function named double that takes an int and returns twice its value. What should double(7) return?
- 2 Given nums := []int{2, 4, 6} followed by nums = append(nums, 8), what are the length of nums and the value at index 3?
- 3 Given scores := map[string]int{"Mia": 88, "Leo": 91}, write the Go expression that checks whether the key "Ava" exists.
- 4 Explain why Go encourages returning error values instead of hiding failures, and describe how this affects program reliability.