Go (Golang) Cheat Sheet

Go programming language essentials — goroutines, channels, interfaces, error handling, and idiomatic patterns for concurrent and systems programming.

Last Updated: July 15, 2025

Go Basics

FeatureExample
Variablesvar name string = "Go" or name := "Go"
Constantsconst MaxRetries = 3
Functionsfunc add(a, b int) int { return a + b }
Multiple returnsfunc divide(a, b float64) (float64, error)

Goroutines & Channels

FeatureExample
Goroutinego doWork() — runs concurrently
Channelch := make(chan int)
Send/Receivech <- 42 send; val := <-ch receive
Selectselect { case msg := <-ch1: ... case <-ch2: ... }

Interfaces

PatternExample
Definetype Writer interface { Write([]byte) (int, error) }
ImplementAny type with a Write method satisfies Writer — implicit, no "implements" keyword
Empty interfaceinterface{} — any type (use any in Go 1.18+)

Error Handling

PatternExample
Standardif err != nil { return err }
Custom errorfmt.Errorf("failed: %w", err) — wraps with %w for unwrapping
Deferdefer file.Close() — runs at function exit
Pro Tip: Go's philosophy: 'Do less, enable more.' The language is deliberately small — 25 keywords. Master goroutines + channels, interfaces, and the standard library. Don't fight the idioms; embrace if err != nil.
← Back to Programming Languages | Browse all categories | View all cheat sheets