๐Ÿน Go

โ† Back to Cheatsheets

A quick-reference cheatsheet for the most commonly used Go features. Go is a statically typed, compiled language designed for simplicity and concurrency. It has no classes or inheritance โ€” behaviour is composed through interfaces and structs. Error handling is explicit, and goroutines make concurrent code straightforward to write.

Resources

go.dev โ€” Official site pkg.go.dev/std โ€” Standard library docs Tour of Go โ€” Interactive tutorial Effective Go โ€” Best practices guide Go by Example

Variables & Types

Go is statically typed โ€” every variable has a type known at compile time. The := short declaration infers the type from the right-hand side and is the most common form inside functions. Variables are zero-valued by default (0, "", false, nil), so there are no undefined surprises.

Slices & Maps

Slices are the primary sequence type โ€” a view over an underlying array with a length and capacity. They grow automatically via append. Maps are hash tables with O(1) average lookup. Both are reference types: assigning a slice or map copies the header, not the data. Always use make or a literal to initialise a map before writing to it.

Control Flow

Go has a single loop keyword, for, which covers C-style loops, while-style loops (for condition), and infinite loops (for {}). range iterates over slices, maps, strings, and channels. defer is Go's primary cleanup mechanism โ€” it runs when the enclosing function returns, in LIFO order.

Functions

Functions are first-class values in Go โ€” they can be assigned to variables, passed as arguments, and returned from other functions. Multiple return values replace exceptions for expected errors. Closures capture variables by reference, so mutations inside the closure affect the outer variable.

Structs & Interfaces

Go uses composition over inheritance. Structs hold data; methods are defined separately on types. Interfaces are satisfied implicitly โ€” any type that implements the required methods qualifies, with no implements keyword. Use pointer receivers when the method needs to mutate the struct or when copying would be expensive.

Error Handling

In Go, errors are values โ€” functions return them as a second return value and callers check immediately. This makes the happy path and error path explicit in the code. Use %w to wrap errors so callers can inspect the cause with errors.Is and errors.As without depending on string matching.

Goroutines & Channels

Goroutines are lightweight threads managed by the Go runtime โ€” you can run thousands concurrently. Channels are typed conduits that let goroutines communicate safely. The Go mantra is "do not communicate by sharing memory; share memory by communicating". Use sync.WaitGroup to wait for goroutines to finish and sync.Mutex when you must share state directly.

Common stdlib

The Go standard library is extensive and high-quality. Most everyday tasks โ€” HTTP, JSON, file I/O, string manipulation, cryptography โ€” need no external packages. Packages are imported by their full path; only the last element is used as the identifier in code.