Advertisement
cwchen

[Go] Closing channels.

Nov 28th, 2017
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.25 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func main() {
  6.     ch := make(chan int, 4)
  7.     ch <- 2
  8.     ch <- 4
  9.     close(ch)
  10.     // ch <- 6 // panic, send on closed channel
  11.  
  12.     fmt.Println(<-ch)
  13.     fmt.Println(<-ch)
  14.     fmt.Println(<-ch) // closed, returns zero value for element
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement