Created
February 27, 2020 13:33
-
-
Save PeteGabriel/0d8a075e65abf20736ddaa2f9c33241b to your computer and use it in GitHub Desktop.
Working with channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
var c chan string | |
var c1 chan int | |
func main() { | |
c = make(chan string) | |
// runtime.GOMAXPROCS(2) uncomment to allow concurrency | |
go ready("Tea", 1) | |
go ready("Coffee", 1) | |
fmt.Println("im waiting") | |
fmt.Println(<-c) | |
fmt.Println(<-c) | |
var vals []int | |
vals = make([]int, 0) | |
c1 = make(chan int) | |
for i:=0; i < 10; { | |
go p(i) | |
n := <-c1 | |
fmt.Println(n) | |
vals = append(vals, n) | |
i++ | |
} | |
} | |
func ready(s string, i int) { | |
time.Sleep(time.Duration(i) * time.Second) | |
c <- s + " is ready!" | |
} | |
func p(i int) { | |
time.Sleep(time.Duration(i) * time.Second) | |
c1 <- i | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment