Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Built-in Functions

Go close() function

Reference for Go close(): signal that no more values will be sent on a channel, what receivers see afterwards, and the panics that closing wrongly causes.

close() marks a channel as closed. It tells every receiver that no more values will ever be sent. Values already in the channel's buffer are still delivered; after they are drained, every receive returns the zero value of the element type immediately.

Closing is a message from the sender side: "I am done". Receivers cannot close a channel they only read from, and you never need to close a channel just to free memory, because the garbage collector handles that. You close a channel when somebody waits for the end of the data, most often a for range loop.

Go
ch := make(chan int, 3)
ch <- 1
close(ch)
v, ok := <-ch // v == 1, ok == true
v, ok = <-ch  // v == 0, ok == false

Try it

Go Close ends a range loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go What closing wrongly does

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func close(c chan<- Type)
Name Description
c A bidirectional or send-only channel. Passing a receive-only channel (<-chan T) is a compile-time error.

Returns: nothing.

Panics:

  • Closing a nil channel panics with close of nil channel.
  • Closing an already closed channel panics with close of closed channel.
  • Sending on a closed channel panics with send on closed channel. A sender blocked on the channel when it is closed panics too.

Usage notes

  • Only the sender closes. If several goroutines send, none of them can safely decide that it is the last. Let a coordinator close the channel after a sync.WaitGroup says every sender has finished.
  • Receiving from a closed channel never blocks. Use the two-value form v, ok := <-ch to tell a real zero value from "closed and empty". ok is false only when the channel is closed and drained.
  • for range stops automatically when the channel is closed and empty. Without a close, the loop blocks forever and the goroutine leaks.
  • Closing is not required. A channel used only to pass values, with receivers that know how many to expect, can simply be dropped.
  • Closing broadcasts. All receivers wake up at once, which makes a chan struct{} that is closed a popular "done" or cancellation signal (this is how context implements Done()).
  • Buffered values survive. Closing does not discard what is already buffered.
  • There is no built-in way to ask whether a channel is closed without receiving from it. Design the protocol so you do not need to.