Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "time"
- )
- func main() {
- var (
- foo_completed bool = false
- bar_completed bool = false
- completed bool = false
- )
- for completed == false {
- sharedChan := make(chan string, 1)
- go foo(sharedChan)
- go bar(sharedChan)
- select {
- case sharedChan <- "starting foo":
- fmt.Println("starting up foo goroutine")
- case sharedChan <- "starting bar":
- fmt.Println("starting up bar goroutine")
- case sharedChan <- "done foo":
- fmt.Println("foo goroutine done")
- foo_completed = true
- case sharedChan <- "done bar":
- fmt.Println("bar goroutine done")
- bar_completed = true
- }
- if foo_completed && bar_completed {
- completed = true;
- }
- }
- fmt.Println("All goroutines have completed.")
- }
- func foo(c chan string) {
- c <- "starting foo"
- time.Sleep(1*time.Minute)
- c <- "done foo"
- }
- func bar(c chan string) {
- c <- "starting bar"
- time.Sleep(2*time.Minute)
- c <- "done bar"
- }
Add Comment
Please, Sign In to add comment