Skip to content

Instantly share code, notes, and snippets.

@olehcambel
Last active April 20, 2020 19:12
Show Gist options
  • Save olehcambel/c2d851b942cec3bd52c3241c3b51a5ea to your computer and use it in GitHub Desktop.
Save olehcambel/c2d851b942cec3bd52c3241c3b51a5ea to your computer and use it in GitHub Desktop.
package main
import (
"log"
"time"
)
func main() {
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(time.Second * 1)
log.Print("data from c1")
c1 <- "one second"
}()
go func() {
time.Sleep(time.Second * 3)
log.Print("data from c2")
c2 <- "three seconds"
}()
for i := 0; i < 2; i++ {
select {
case x1 := <-c1:
log.Printf("result from c1: %v", x1)
case x2 := <-c2:
log.Printf("result from c2: %v", x2)
}
}
}
package main
import (
"log"
"time"
)
func main() {
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(time.Second * 1)
x1 := <-c1
log.Printf("result from c1: %v", x1)
}()
go func() {
time.Sleep(time.Second * 3)
x2 := <-c2
log.Printf("result from c2: %v", x2)
}()
for i := 0; i < 2; i++ {
select {
case c1 <- "one second":
log.Printf("sent one")
case c2 <- "three seconds":
log.Printf("sent three")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment