Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.80 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "time"
  6. )
  7.  
  8. func GoroutineTwo() {
  9.     var channelOne chan string = make(chan string)
  10.     var channelTwo chan string = make(chan string)
  11.  
  12.     go func() {
  13.         for {
  14.             channelOne <- "From channel one."
  15.             time.Sleep(time.Second * 2)
  16.         }
  17.     }()
  18.  
  19.     go func() {
  20.         for {
  21.             channelTwo <- "From channel two."
  22.             time.Sleep(time.Second * 3)
  23.         }
  24.     }()
  25.  
  26.     go func() {
  27.         for {
  28.             select {
  29.             case messageOne := <-channelOne:
  30.                 fmt.Println(messageOne)
  31.             case messageTwo := <-channelTwo:
  32.                 fmt.Println(messageTwo)
  33.             case timeout := <-time.After(time.Second):
  34.                 fmt.Println("[", timeout, "] No messages ready.")
  35.             }
  36.         }
  37.     }()
  38.  
  39.     // Wait for input so the program doesn't close
  40.     // abruptly after it finished running the program.
  41.     var input string
  42.     fmt.Scanln(&input)
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement