var x int = 10 Variable with explicit type
x := 10 Short variable declaration (inferred)
const Pi = 3.14159 Constant declaration
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 Integer types
float32 float64 Floating point types
string bool byte rune Other basic types
if x > 0 {
...
} else if x == 0 {
...
} else {
...
} If/else (no parentheses needed)
for i := 0; i < 10; i++ { ... } Classic for loop
for _, v := range slice { ... } Range over slice/map/string
for condition { ... } While-style loop
for { ... } Infinite loop
switch x {
case 1:
...
case 2, 3:
...
default:
...
} Switch (no fallthrough by default)
fmt.Println("hello") Print with newline
fmt.Sprintf("name: %s, age: %d", name, age) Format string
strconv.Atoi(s) / strconv.Itoa(n) String <-> int conversion
s := []int{1, 2, 3} Create a slice literal
s := make([]int, 0, 10) Create slice with length and capacity
s = append(s, 4, 5) Append elements to slice
s[1:3] Slice of slice (index 1 to 2)
len(s) / cap(s) Length and capacity of slice
copy(dst, src) Copy elements between slices
s = append(s[:i], s[i+1:]...) Delete element at index i
sort.Ints(s) / sort.Strings(s) Sort slice in-place
slices.Contains(s, val) Check if slice contains value (Go 1.21+)
m := map[string]int{"a": 1} Create a map literal
m := make(map[string]int) Create empty map
m["key"] = value Set map value
val, ok := m["key"] Get value with existence check
delete(m, "key") Delete key from map
for k, v := range m { ... } Iterate over map entries
func add(a, b int) int {
return a + b
} Function with return type
func divide(a, b float64) (float64, error) Multiple return values
func greet(name string, opts ...string) Variadic function
result, err := divide(10, 3) Capture multiple returns
fn := func(x int) int { return x * 2 } Anonymous function / closure
defer file.Close() Defer execution until function returns
func init() { ... } Package initialization function
type User struct {
Name string
Age int
} Struct type definition
u := User{Name: "Alice", Age: 30} Struct literal
u := &User{Name: "Alice"} Pointer to struct
func (u User) Greet() string {
return "Hi " + u.Name
} Method on struct (value receiver)
func (u *User) SetName(n string) {
u.Name = n
} Method with pointer receiver (mutate)
type Admin struct {
User
Role string
} Struct embedding (composition)
type Config struct {
Host string `json:"host"`
} Struct tags (JSON, DB, etc.)
type Reader interface {
Read(p []byte) (n int, err error)
} Interface definition
type Stringer interface {
String() string
} Stringer interface (like toString)
interface{} / any Empty interface (accepts any type)
val, ok := i.(ConcreteType) Type assertion with check
switch v := i.(type) {
case string:
...
case int:
...
} Type switch
go func() { ... }() Launch goroutine
ch := make(chan int) Create unbuffered channel
ch := make(chan int, 10) Create buffered channel (cap 10)
ch <- value Send value to channel
value := <-ch Receive value from channel
close(ch) Close channel (no more sends)
select {
case v := <-ch1:
...
case ch2 <- val:
...
default:
...
} Select on multiple channels
var mu sync.Mutex
mu.Lock()
defer mu.Unlock() Mutex for shared state
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
...
}()
wg.Wait() WaitGroup for goroutine coordination
if err != nil {
return fmt.Errorf("failed: %w", err)
} Wrap and propagate error
errors.New("something failed") Create simple error
fmt.Errorf("user %d: %w", id, err) Create formatted error (wrapping)
errors.Is(err, target) Check if error matches target
errors.As(err, &target) Extract typed error from chain
type MyError struct {
Code int
Msg string
}
func (e *MyError) Error() string {
return e.Msg
} Custom error type
panic("fatal") / recover() Panic and recover (rare, avoid)
Go (Golang) Cheat Sheet — Goroutines, Channels, Interfaces & Error Handling
Go appeared in 2009 with an explicit aim of staying small — short keyword list, gofmt-enforced style, a concurrency model built around goroutines and channels. The cheatsheet below covers 60+ snippets across basics, slices and maps, functions, structs, interfaces, concurrency, and error handling. Most trouble in Go code does not come from forgetting syntax. It comes from quirks that look ordinary until they bite. An interface value holding a typed nil pointer is itself not nil, so `if err != nil` returns true on an `err` assigned `var p *MyError; return p` — the interface still carries the type. A slice and its `append` may share the same backing array or not depending on capacity, so appending to a passed slice can mutate the caller's data or quietly stop reflecting it. Map iteration order is deliberately randomised between runs, so a test depending on insertion order passes nine times and fails on the tenth. These are the snippets pulled up while debugging that — the right nil check, the right append pattern, the right channel direction — without rereading the spec each time.
Common pitfalls in Go
A few patterns earn their place near the top of any Go file. Return `nil` explicitly when there is no error rather than a typed nil through an interface, because `return (*MyError)(nil)` against an `error` return type produces a non-nil interface that bypasses `if err != nil` checks downstream. Wrap propagated errors with `fmt.Errorf("context: %w", err)` so callers can later use `errors.Is` and `errors.As` to inspect the chain — a plain `%v` loses that information forever. `defer` evaluates its arguments at the point the `defer` statement runs, not when the deferred call actually fires, so `defer log.Print(time.Now())` logs the start time and not the end. The `for _, v := range slice` loop reuses the same `v` variable across iterations, which is fine until a goroutine captures it by reference inside the loop (Go 1.22 made each iteration get its own scope, but pre-1.22 code still in production needs the explicit `v := v` shadow). The cheatsheet groups all of this into Basics, Slices & Maps, Functions, Structs, Interfaces, Concurrency, and Error Handling so the right section is one click away.
- 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
Free. No signup. Your inputs stay in your browser. Ads via Google AdSense (consent required).