Channels

Contents
Make and send
Receive and close
Range until closed
Buffered channels

Make and send

Channels move values between goroutines safely, with no shared memory involved. make creates a channel for one element type. The send operator pushes a value in, and a receive pulls it out. An unbuffered send waits until someone receives, which keeps the two sides in step.

package main import "fmt" func main() { ch := make(chan string) go func() { ch <- "ping" }() fmt.Println(<-ch) }

ping

The sender blocks until main receives, so nothing is lost and nothing races. Swap the order and the program still works. This handshake is the heartbeat of Go concurrency.

Receive and close

Receivers need to know when no more values will come. The sender closes the channel to say it is done, and each receive reports ok as false past that point. Check that flag to stop cleanly. Only the sender ever closes, exactly once.

package main import "fmt" func main() { ch := make(chan int) go func() { for i := 1; i <= 3; i++ { ch <- i } close(ch) }() for { v, ok := <-ch if !ok { break } fmt.Println(v) } }

1
2
3

Sending on a closed channel panics, so ownership of close must be clear. Receiving from a closed channel is safe and yields zero values. The range form below hides the flag check entirely.

Range until closed

range over a channel reads values until the channel closes, then exits the loop by itself. The shape mirrors a slice loop and reads well. Forgetting close here hangs the loop forever, which is the most common channel bug. Pair every ranging consumer with a closing producer.

package main import "fmt" func main() { ch := make(chan int) go func() { for i := 1; i <= 3; i++ { ch <- i } close(ch) }() for v := range ch { fmt.Println(v) } fmt.Println("done") }

1
2
3
done

Buffered channels

Buffered channels hold a few values without a waiting receiver. make takes a capacity, and sends block only when the buffer is full. Small buffers smooth out bursts between fast producers and slow consumers. Zero extra goroutines are needed for the example below.

package main import "fmt" func main() { ch := make(chan string, 2) ch <- "a" ch <- "b" fmt.Println(<-ch) fmt.Println(<-ch) fmt.Println(len(ch), cap(ch)) }

a
b
0 2

Both sends succeed at once because capacity is two. A third send would block until a receive frees a slot. Buffers tune timing, while close still signals the end of data.

Next: select and sync

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: