Advertisement
milton19

smallexample.go

May 14th, 2018
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.75 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "math/rand"
  5.     "time"
  6. )
  7.  
  8. func Sender(intChannel chan <- int , stringChannel chan <- string, end chan <- bool) {
  9.     randInt := rand.Int()
  10.     if randInt%2 == 0 {
  11.         intChannel <- randInt
  12.     } else  {
  13.         stringChannel <- "odd"
  14.     }
  15.     end <- true
  16. }
  17.  
  18. func Receiver(intChannel <- chan  int , stringChannel <- chan  string, end chan <- bool) {
  19.     select {
  20.         case val:= <-intChannel :
  21.             println("Received ",val)
  22.         case val := <-stringChannel :
  23.             println("Received ",val)
  24.     }
  25.     end <- true
  26. }
  27.  
  28. func main() {
  29.     rand.Seed(time.Now().UTC().UnixNano())
  30.     intChannel, stringChannel, end := make(chan int), make(chan string), make(chan bool)
  31.     go Sender(intChannel,stringChannel,end)
  32.     go Receiver(intChannel,stringChannel,end)
  33.     <-end
  34.     <-end
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement