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.
Understanding Go (Golang) Quick Reference
Go is designed to make programs predictable when they grow beyond a few lines. Its compiler checks types before the program runs. This catches many mistakes early, such as passing text to code that expects a number.
Go also has clear rules about imports, names, and formatting. The standard formatter, called gofmt, rewrites spacing and indentation in one common style. This may feel strict at first, but it makes school projects easier to read and easier to compare with examples.
When code fails to build, read the first error carefully. Later errors are often results of the first problem.
Slices need special care because they behave like a view into an underlying block of data. Two slices can sometimes refer to the same data. Changing an item through one slice may then change what the other slice shows.
Adding items can create a new underlying block when there is not enough room, so a slice should usually be treated as the updated result after adding. Maps are useful for lookups such as student marks by name, inventory by product code, or word counts in a text file.
A map lookup can fail without stopping the program. Checking whether a key was found prevents a missing value from being confused with a real zero value.
Structs let a program group related information into one custom type. A student record might hold a name, an age, and a score. Methods give that type useful behavior, such as calculating a grade or printing a summary.
Interfaces work differently. They describe behavior rather than a fixed kind of data. If several types can perform the same action, code can work with the interface instead of needing separate logic for each type.
This becomes useful when building larger programs, but students should first be comfortable tracing values through ordinary functions and structs. Clear names matter more than clever designs.
Goroutines allow a program to start work that can run independently, such as reading several files, handling network requests, or updating a display while another task waits. They are lightweight, but shared data can create difficult bugs. A race happens when multiple goroutines access the same changing data without safe coordination.
Channels provide one way to pass values between goroutines so that tasks communicate instead of freely changing the same variables. Start with small examples and make sure every goroutine has a clear stopping point. Error handling matters here too.
A returned error is normal information about a failed operation, such as a missing file or unavailable connection. Check it close to where it is returned, then either fix the problem, report it clearly, or pass it back to the caller.