Skip to content

Instantly share code, notes, and snippets.

@PeteGabriel
Created February 27, 2020 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeteGabriel/0d8a075e65abf20736ddaa2f9c33241b to your computer and use it in GitHub Desktop.
Save PeteGabriel/0d8a075e65abf20736ddaa2f9c33241b to your computer and use it in GitHub Desktop.
Working with channels
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