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.
Variables & Constants 7
var name string = "value"name := "value"const Pi = 3.14var (name string; age int)var arr [5]intslice := []int{1, 2, 3}m := map[string]int{"a": 1}Control Flow 8
if condition { } else { }if err := fn(); err != nil { }switch x { case 1: case 2: default: }for i := 0; i < 10; i++ { }for condition { }for { }for i, v := range slice { }for k, v := range map { }Functions 6
func add(a, b int) int { return a + b }func swap(a, b int) (int, int)func sum(nums ...int) intdefer fn()panic("error")recover()Struct & Method 5
type User struct { Name string; Age int }u := User{Name: "tom", Age: 20}func (u User) String() stringfunc (u *User) SetName(n string)u := &User{}Interface 5
type Reader interface { Read(p []byte) (int, error) }var r Reader = &File{}type Empty interface{}v, ok := i.(string)switch v := i.(type) { }Concurrency 8
go fn()ch := make(chan int)ch := make(chan int, 10)ch <- valuevalue := <-chclose(ch)select { case <-ch1: case <-ch2: }var wg sync.WaitGroupError Handling 5
if err != nil { return err }type MyError struct { msg string }func (e *MyError) Error() stringerrors.New("message")fmt.Errorf("failed: %w", err)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