Close function
In Go, close is a built-in function that marks a channel closed, preventing further sends and allowing receivers to detect completion.
Learn Go with Pocket-Sized Projects
The close function is a Go built-in that marks a channel as closed so that no more values can be sent on it and receivers can detect completion.
- How it is used
- Uses channel closure to mark completion and broadcast a quit signal to concurrent workers.
- How deep it goes
- Explains closed-channel reads, zero values and the receive operator’s second result, then uses closure in a select-based cancellation pattern.
- Examples
- Uses a goroutine completion notification and a concurrent maze solver’s quit channel.
- What you can do after
- Apply it using Go’s close built-in to signal completion and coordinate goroutines.
Read how Learn Go with Pocket-Sized Projects explains close function →
Learn Concurrent Programming with Go
close is a Go built-in function that marks a channel as closed so that no further values can be sent to it.
- How it is used
- Signals that a channel has no more values and lets receivers terminate cleanly.
- How deep it goes
- Explains closure, post-closure reads, the open flag, range loops, and pipeline flushing, but not channel implementation internals.
- Examples
- Uses integer messages, comma-ok receives, range loops, and a longest-words pipeline that flushes results on closure.
- What you can do after
- Apply it by closing channels after final sends and stopping receivers with the open flag or a range loop.
Read how Learn Concurrent Programming with Go explains close function →
Go in Practice, Second Edition
The Go close function marks a channel as unable to accept further sent values and enables receivers to observe that it is closed.
- How it is used
- Coordinates graceful shutdown between goroutines communicating through channels.
- How deep it goes
- Explains send-after-close panics, zero-value receives, comma-ok detection, and a done-channel shutdown pattern.
- Examples
- Uses timeout-driven programs, an improper close panic, comma-ok reception, and a done-channel implementation.
- What you can do after
- Apply it via coordinated sender-side closure and diagnose panics caused by sends racing with closure.
Read how Go in Practice, Second Edition explains close function →
What to know first
- goroutineA goroutine is a lightweight function execution unit managed by the Go runtime that runs concurrently with other program activity.
- slicingSlicing selects or partitions a portion of an indexed sequence or multidimensional array, often along specified indices or dimensions.
- zero valueA zero value is the default, type-dependent value assigned when a variable, field, or newly allocated value has no explicit initial value.
Related concepts
- closed channelproduces · A closed channel is a Go channel that accepts no further sends while allowing receives to complete, including delivery of buffered values.
- channelused by · A channel is a medium or logical pathway for conveying information, messages, or other content between sources and recipients.
- channelconfigures · A channel is a typed Go communication and synchronization mechanism through which goroutines send and receive values.
- deferused by · Defer is a language feature that schedules code to execute when its enclosing scope or function exits.
- range clauseaddresses · A range clause iterates over elements of an array, slice, map, or channel and assigns the iteration results to loop variables.
Questions about close function
What is close function?
In Go, close is a built-in function that marks a channel closed, preventing further sends and allowing receivers to detect completion.
Which Manning books explain close function?
Learn Go with Pocket-Sized Projects, Learn Concurrent Programming with Go and Go in Practice, Second Edition.
What should I know before learning close function?
Manning books that teach close function usually introduce goroutine, slicing and zero value first.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


