Go Goroutines & Channels Cheat Sheet
Covers launching goroutines with the go keyword, sending and receiving on channels, buffered versus unbuffered channels, and select statements.
2 PagesIntermediateApr 15, 2026
Basic Goroutines
Starting concurrent work with the go keyword.
go
func sayHello() { fmt.Println("Hello from goroutine")}func main() { go sayHello() // Starts a new goroutine (non-blocking) time.Sleep(100 * time.Millisecond) // Naive wait; use sync primitives in real code}
Channels
Typed pipes for communicating between goroutines.
go
ch := make(chan int) // Unbuffered channelgo func() { ch <- 42 // Send value (blocks until received)}()value := <-ch // Receive value (blocks until sent)buffered := make(chan string, 3) // Buffered channel, capacity 3buffered <- "a"buffered <- "b"close(buffered) // Close when no more values will be sentfor v := range buffered { // Range reads until the channel is closed fmt.Println(v)}
sync.WaitGroup & select
Waiting for goroutines and multiplexing channel operations.
go
var wg sync.WaitGroupfor i := 0; i < 3; i++ { wg.Add(1) go func(id int) { defer wg.Done() fmt.Println("worker", id) }(i)}wg.Wait() // Block until all goroutines call Done()select {case msg := <-ch1: fmt.Println("from ch1:", msg)case msg := <-ch2: fmt.Println("from ch2:", msg)case <-time.After(1 * time.Second): fmt.Println("timeout")default: fmt.Println("no message ready")}
Concepts
Vocabulary for Go's concurrency model.
- go keyword- Launches a function call as a new lightweight goroutine, managed by the Go runtime
- Unbuffered channel- Send blocks until a receiver is ready (a synchronous handoff)
- Buffered channel- Send blocks only once the buffer is full
- close(ch)- Signals no more values will be sent; receiving from a closed channel returns the zero value and false
- sync.WaitGroup- Waits for a collection of goroutines to finish (Add/Done/Wait)
- sync.Mutex- Protects shared state from concurrent access (Lock/Unlock)
- select- Waits on multiple channel operations, choosing pseudo-randomly if more than one is ready
Pro Tip
"Don't communicate by sharing memory; share memory by communicating" — prefer channels to pass ownership of data between goroutines instead of protecting shared variables with mutexes wherever practical.
Was this cheat sheet helpful?
Explore Topics
#GoGoroutinesChannels#GoGoroutinesChannelsCheatSheet#Programming#Intermediate#BasicGoroutines#Channels#SyncWaitGroupSelect#Concepts#Concurrency#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance