Go Cheatsheet

Go Cheatsheet
var x int = 10

Variable with explicit type

Basics
x := 10

Short variable declaration (inferred)

Basics
const Pi = 3.14159

Constant declaration

Basics
int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64

Integer types

Basics
float32 float64

Floating point types

Basics
string bool byte rune

Other basic types

Basics
if x > 0 { ... } else if x == 0 { ... } else { ... }

If/else (no parentheses needed)

Basics
for i := 0; i < 10; i++ { ... }

Classic for loop

Basics
for _, v := range slice { ... }

Range over slice/map/string

Basics
for condition { ... }

While-style loop

Basics
for { ... }

Infinite loop

Basics
switch x { case 1: ... case 2, 3: ... default: ... }

Switch (no fallthrough by default)

Basics
fmt.Println("hello")

Print with newline

Basics
fmt.Sprintf("name: %s, age: %d", name, age)

Format string

Basics
strconv.Atoi(s) / strconv.Itoa(n)

String <-> int conversion

Basics
s := []int{1, 2, 3}

Create a slice literal

Slices & Maps
s := make([]int, 0, 10)

Create slice with length and capacity

Slices & Maps
s = append(s, 4, 5)

Append elements to slice

Slices & Maps
s[1:3]

Slice of slice (index 1 to 2)

Slices & Maps
len(s) / cap(s)

Length and capacity of slice

Slices & Maps
copy(dst, src)

Copy elements between slices

Slices & Maps
s = append(s[:i], s[i+1:]...)

Delete element at index i

Slices & Maps
sort.Ints(s) / sort.Strings(s)

Sort slice in-place

Slices & Maps
slices.Contains(s, val)

Check if slice contains value (Go 1.21+)

Slices & Maps
m := map[string]int{"a": 1}

Create a map literal

Slices & Maps
m := make(map[string]int)

Create empty map

Slices & Maps
m["key"] = value

Set map value

Slices & Maps
val, ok := m["key"]

Get value with existence check

Slices & Maps
delete(m, "key")

Delete key from map

Slices & Maps
for k, v := range m { ... }

Iterate over map entries

Slices & Maps
func add(a, b int) int { return a + b }

Function with return type

Functions
func divide(a, b float64) (float64, error)

Multiple return values

Functions
func greet(name string, opts ...string)

Variadic function

Functions
result, err := divide(10, 3)

Capture multiple returns

Functions
fn := func(x int) int { return x * 2 }

Anonymous function / closure

Functions
defer file.Close()

Defer execution until function returns

Functions
func init() { ... }

Package initialization function

Functions
type User struct { Name string Age int }

Struct type definition

Structs
u := User{Name: "Alice", Age: 30}

Struct literal

Structs
u := &User{Name: "Alice"}

Pointer to struct

Structs
func (u User) Greet() string { return "Hi " + u.Name }

Method on struct (value receiver)

Structs
func (u *User) SetName(n string) { u.Name = n }

Method with pointer receiver (mutate)

Structs
type Admin struct { User Role string }

Struct embedding (composition)

Structs
type Config struct { Host string `json:"host"` }

Struct tags (JSON, DB, etc.)

Structs
type Reader interface { Read(p []byte) (n int, err error) }

Interface definition

Interfaces
type Stringer interface { String() string }

Stringer interface (like toString)

Interfaces
interface{} / any

Empty interface (accepts any type)

Interfaces
val, ok := i.(ConcreteType)

Type assertion with check

Interfaces
switch v := i.(type) { case string: ... case int: ... }

Type switch

Interfaces
go func() { ... }()

Launch goroutine

Concurrency
ch := make(chan int)

Create unbuffered channel

Concurrency
ch := make(chan int, 10)

Create buffered channel (cap 10)

Concurrency
ch <- value

Send value to channel

Concurrency
value := <-ch

Receive value from channel

Concurrency
close(ch)

Close channel (no more sends)

Concurrency
select { case v := <-ch1: ... case ch2 <- val: ... default: ... }

Select on multiple channels

Concurrency
var mu sync.Mutex mu.Lock() defer mu.Unlock()

Mutex for shared state

Concurrency
var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() ... }() wg.Wait()

WaitGroup for goroutine coordination

Concurrency
if err != nil { return fmt.Errorf("failed: %w", err) }

Wrap and propagate error

Error Handling
errors.New("something failed")

Create simple error

Error Handling
fmt.Errorf("user %d: %w", id, err)

Create formatted error (wrapping)

Error Handling
errors.Is(err, target)

Check if error matches target

Error Handling
errors.As(err, &target)

Extract typed error from chain

Error Handling
type MyError struct { Code int Msg string } func (e *MyError) Error() string { return e.Msg }

Custom error type

Error Handling
panic("fatal") / recover()

Panic and recover (rare, avoid)

Error Handling
Showing 65 of 65 snippets

Free Online Go Cheatsheet

A searchable Go (Golang) quick reference covering basics, slices, maps, functions, structs, interfaces, goroutines, channels, and error handling. Copy any snippet with one click.

About this cheatsheet

A comprehensive Go cheatsheet covering the language's core features and concurrency model.

  • 60+ practical Go snippets
  • 7 categories from basics to concurrency
  • Goroutines and channels explained
  • Error handling patterns
  • One-click copy to clipboard
  • Struct methods and interfaces covered

100% free. No signup required. No data collected or sent anywhere.

Explore All Tools