Go Cheat Sheet
Last updated
Hello World & program structure
Every Go program lives in a package; the entry point is func main in package main.
| Operation | Syntax |
|---|---|
| Package declaration | package main |
| Import a package | import "fmt" |
| Import multiple packages | import (\n "fmt"\n "os"\n) |
| Entry point | func main() { ... } |
| Print a line | fmt.Println("Hello, World!") |
| Formatted print | fmt.Printf("%d items\n", n) |
| Run a file | go run main.go |
| Build a binary | go build |
Variables & types
Go is statically typed but can infer types with :=.
| Operation | Syntax |
|---|---|
| Declare with type | var age int = 30 |
| Declare with inference | var name = "Ada" |
| Short declaration | count := 0 |
| Multiple at once | x, y := 1, 2 |
| Constant | const Pi = 3.14 |
| Zero values | 0, "", false, nil |
| Basic types | int, float64, string, bool, byte, rune |
| Type conversion | f := float64(i) |
Control flow
Go has one loop keyword - for - and no parentheses around conditions.
| Operation | Syntax |
|---|---|
| If / else | if x > 0 { ... } else { ... } |
| If with init statement | if v, ok := m[k]; ok { ... } |
| For loop (C-style) | for i := 0; i < 10; i++ { ... } |
| While-style loop | for x < 100 { ... } |
| Infinite loop | for { ... } |
| Range over a slice | for i, v := range items { ... } |
| Switch | switch day { case 1: ...; default: ... } |
| Switch with no expression | switch { case x > 0: ... } |
| Break / continue | break, continue |
Functions
Functions are first-class values and can return multiple results.
| Operation | Syntax |
|---|---|
| Basic function | func add(a int, b int) int { return a + b } |
| Shared param type | func add(a, b int) int { ... } |
| Multiple return values | func divmod(a, b int) (int, int) { return a / b, a % b } |
| Named return values | func split(sum int) (x, y int) { x = sum / 2; return } |
| Variadic function | func sum(nums ...int) int { ... } |
| Anonymous function | f := func() { ... } |
| Closure | func counter() func() int { ... } |
| Defer (runs on return) | defer file.Close() |
Slices & arrays
Arrays have a fixed size; slices are dynamic views over arrays.
| Operation | Syntax |
|---|---|
| Fixed-size array | var a [3]int |
| Slice literal | nums := []int{1, 2, 3} |
| Make a slice | s := make([]int, 0, 10) |
| Append | s = append(s, 4) |
| Length / capacity | len(s), cap(s) |
| Slice a slice | s[1:3] |
| Copy | copy(dst, src) |
| Iterate | for i, v := range s { ... } |
Maps
Maps are Go's built-in hash tables.
| Operation | Syntax |
|---|---|
| Make a map | m := make(map[string]int) |
| Map literal | m := map[string]int{"a": 1, "b": 2} |
| Set a value | m["c"] = 3 |
| Get a value | v := m["a"] |
| Check existence | v, ok := m["x"] |
| Delete a key | delete(m, "a") |
| Length | len(m) |
| Iterate | for k, v := range m { ... } |
Structs & methods
Structs group fields; methods attach behavior via a receiver.
| Operation | Syntax |
|---|---|
| Define a struct | type Point struct { X, Y int } |
| Create an instance | p := Point{X: 1, Y: 2} |
| Access a field | p.X |
| Pointer to a struct | p := &Point{1, 2} |
| Value receiver method | func (p Point) Dist() float64 { ... } |
| Pointer receiver method | func (p *Point) Move(dx int) { p.X += dx } |
| Embedded struct | type Circle struct { Point; R int } |
| Struct tags | Name string with a backtick-quoted json:"name" tag |
Interfaces
Interfaces are satisfied implicitly - any type with the right methods qualifies.
| Operation | Syntax |
|---|---|
| Define an interface | type Shape interface { Area() float64 } |
| Implement (implicit) | func (c Circle) Area() float64 { ... } |
| Empty interface (any) | var x interface{} or var x any |
| Type assertion | s, ok := x.(string) |
| Type switch | switch v := x.(type) { case int: ... } |
| Stringer interface | func (p Point) String() string { ... } |
Goroutines & channels
Goroutines are lightweight threads; channels pass values between them.
| Operation | Syntax |
|---|---|
| Start a goroutine | go doWork() |
| Make a channel | ch := make(chan int) |
| Buffered channel | ch := make(chan int, 5) |
| Send to a channel | ch <- 42 |
| Receive from a channel | v := <-ch |
| Close a channel | close(ch) |
| Range over a channel | for v := range ch { ... } |
| Select on channels | select { case v := <-ch: ...; default: ... } |
| Wait group | var wg sync.WaitGroup; wg.Add(1); wg.Wait() |
| Mutex | var mu sync.Mutex; mu.Lock(); mu.Unlock() |
Error handling
Go returns errors as values - there are no exceptions.
| Operation | Syntax |
|---|---|
| Return an error | func read() (string, error) { return "", err } |
| Check an error | if err != nil { return err } |
| Create an error | errors.New("not found") |
| Formatted error | fmt.Errorf("bad id %d", id) |
| Wrap an error | fmt.Errorf("load: %w", err) |
| Unwrap / match | errors.Is(err, os.ErrNotExist) |
| Match by type | errors.As(err, &target) |
| Panic / recover | defer func() { recover() }() |
The Go syntax you reach for most, on one page. This Go cheat sheet is a quick reference for the core language - variables and types, slices and maps, structs and interfaces, plus the goroutines and channels that make Golang shine at concurrency.
Everything here is standard Go and compiles with the official toolchain. Copy what you need, or try every snippet live in the Go playground - no install required.
Go cheat sheet FAQ
Is this Go cheat sheet free?
What is the difference between := and var in Go?
var x = 0 declares a variable and works anywhere, including at package level. x := 0 is short variable declaration: it both declares and assigns, infers the type, and can only be used inside a function. Use var when you need an explicit type or a package-level variable, and := for concise local declarations.What are goroutines and channels?
go someFunc(). Channels are typed pipes that let goroutines communicate safely: one goroutine sends with ch <- v and another receives with <-ch. Together they are Go's core concurrency model, captured by the saying "share memory by communicating."