GO

Go Cheatsheet

Core Go syntax - types, goroutines/channels, and the error-handling pattern used throughout idiomatic Go code.

Basics

var x int = 5

Typed variable declaration

x := 5

Short variable declaration with inference

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

Function declaration

type Point struct { X, Y int }

Struct declaration

slice := []int{1, 2, 3}

Slice literal

Error handling

val, err := doSomething()

Idiomatic multi-return with error

if err != nil { return err }

Standard error check

errors.New("message")

Create a simple error

fmt.Errorf("failed: %w", err)

Wrap an error with context

Concurrency

go doSomething()

Start a goroutine

ch := make(chan int)

Create a channel

ch <- value

Send a value on a channel

val := <-ch

Receive a value from a channel

var wg sync.WaitGroup

Wait for goroutines to finish