Built-in function

Go close()

Marks a channel as having no more sends. Receivers can drain buffered values, then receive zero values with ok=false. Sending to a closed channel, closing it again or closing a nil channel panics; a receive-only channel cannot be closed. Coordinate a single closure owner. Example prints 7 true then 0 false.

Formal Syntax & Structure

go syntaxgo
close(ch)

Parameters & Attributes

NameTypeDescription
chsend-capable channelChannel to close.

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  ch := make(chan int, 1)  ch <- 7  close(ch)  v, ok := <-ch  fmt.Println(v, ok)  v, ok = <-ch  fmt.Println(v, ok)}
Go Reference