Goroutines and Channels
Goroutines and Channels in Go
Go's concurrency model is built on goroutines (lightweight threads) and channels (typed communication pipes).
Goroutines
Launch with the go keyword:
go someFunction()
go func() { … }()Goroutines are much cheaper than OS threads — you can spawn thousands.
Channels Channels pass data between goroutines safely:
ch := make(chan int) // unbuffered
ch := make(chan int, 10) // buffered (capacity 10)
ch <- value // send
value := <-ch // receiveUnbuffered vs buffered
- Unbuffered: send blocks until a receiver is ready (synchronizes goroutines).
- Buffered: send blocks only when the buffer is full.
range over channel Receive values until the channel is closed:
for v := range ch { … }close
Signals no more values will be sent. Receivers see remaining values then the zero value with ok == false.
Key points:
- Never close a channel from the receiver side.
- Sending on a closed channel panics.
- Goroutines are not threads — the Go scheduler multiplexes them onto OS threads.
Code Examples
package main
import (
"fmt"
)
func squares(n int, ch chan<- int) {
for i := 1; i <= n; i++ {
ch <- i * i
}
close(ch)
}
func main() {
ch := make(chan int)
go squares(5, ch)
for v := range ch {
fmt.Print(v, " ")
}
fmt.Println()
}squares runs in a goroutine, sending values to ch. The main goroutine reads with range until ch is closed.
package main
import (
"fmt"
"sync"
)
func worker(id int, wg *sync.WaitGroup, sem chan struct{}) {
defer wg.Done()
sem <- struct{}{} // acquire slot
fmt.Printf("Worker %d running\n", id)
<-sem // release slot
}
func main() {
const maxConcurrent = 2
sem := make(chan struct{}, maxConcurrent)
var wg sync.WaitGroup
for i := 1; i <= 4; i++ {
wg.Add(1)
go worker(i, &wg, sem)
}
wg.Wait()
fmt.Println("All done")
}A buffered channel with capacity N acts as a semaphore, limiting concurrent access. WaitGroup waits for all goroutines to finish.
Quick Quiz
1. What happens when you send on an unbuffered channel with no receiver ready?
2. What does `close(ch)` signal to receivers?
Was this lesson helpful?