daily pastebin goal
18%
SHARE
TWEET

[Go] Closing channels.

cwchen Nov 28th, 2017 136 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
Top