Go Cheatsheet - Go Syntax & Concurrency Reference

This reference is for developers writing Go API services, concurrent workers, or ops tooling, collecting the most common syntax skeletons: variable declarations, loops and branches, functions and defer, struct methods, interfaces, and the goroutine/channel concurrency that is easiest to get wrong. Unlike bare API docs, entries give copy-ready snippets grouped by the problem each solves. After reading you should be able to write concurrency with goroutines + a WaitGroup, abstract dependencies through interfaces, and check errors Go-style with err != nil.

Languages·44 commands·Last updated 2026-07-21
gogolangProgrammingConcurrency

Variables & Constants 7

var name string = "value"
Explicit variable declaration
name := "value"
Short variable declaration (type inferred)
const Pi = 3.14
Declare a constant
var (name string; age int)
Declare multiple variables
var arr [5]int
Declare an array
slice := []int{1, 2, 3}
Declare a slice
m := map[string]int{"a": 1}
Declare a map

Control Flow 8

if condition { } else { }
if statement
if err := fn(); err != nil { }
if with initializer
switch x { case 1: case 2: default: }
switch statement
for i := 0; i < 10; i++ { }
for loop
for condition { }
while-style loop
for { }
infinite loop
for i, v := range slice { }
iterate over slice/array
for k, v := range map { }
iterate over map

Functions 6

func add(a, b int) int { return a + b }
Plain function
func swap(a, b int) (int, int)
Multiple return values
func sum(nums ...int) int
Variadic parameters
defer fn()
defer execution (runs on function exit)
panic("error")
panic (crashes the program)
recover()
recover from panic (used in defer)

Struct & Method 5

type User struct { Name string; Age int }
Define a struct
u := User{Name: "tom", Age: 20}
Create a struct instance
func (u User) String() string
Value receiver method
func (u *User) SetName(n string)
Pointer receiver method
u := &User{}
Create a struct pointer

Interface 5

type Reader interface { Read(p []byte) (int, error) }
Define an interface
var r Reader = &File{}
Interface satisfied implicitly (no implements keyword)
type Empty interface{}
Empty interface (holds any type)
v, ok := i.(string)
Type assertion
switch v := i.(type) { }
Type switch

Concurrency 8

go fn()
Start a goroutine
ch := make(chan int)
Create an unbuffered channel
ch := make(chan int, 10)
Create a buffered channel
ch <- value
Send a value to a channel
value := <-ch
Receive a value from a channel
close(ch)
Close a channel
select { case <-ch1: case <-ch2: }
Multiplex with select
var wg sync.WaitGroup
WaitGroup to wait for goroutines

Error Handling 5

if err != nil { return err }
Standard error check
type MyError struct { msg string }
Custom error type
func (e *MyError) Error() string
Implement the error interface
errors.New("message")
Create an error
fmt.Errorf("failed: %w", err)
Wrap an error (Go 1.13+)

Tips

  • Go's error handling is explicit: every function that may return an error must be checked with err != nil.
  • goroutines are extremely lightweight (a few KB), so you can easily spawn hundreds of thousands.
  • Channels are the primary way goroutines communicate, following "don't communicate by sharing memory, share memory by communicating".

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us