Skip to content

Instantly share code, notes, and snippets.

@hkarim
Created March 23, 2014 00:45
Show Gist options
  • Save hkarim/9716664 to your computer and use it in GitHub Desktop.
Save hkarim/9716664 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func expensiveOp() <-chan int {
fmt.Println("expensiveOp...")
time.Sleep(50000000)
c := make(chan int, 1)
c <- 1
return c
}
func veryExpensiveOp(i int) <-chan int {
fmt.Println("veryExpensiveOp...")
time.Sleep(50000000)
c := make(chan int, 1)
c <- i + 1
return c
}
/*
def expensiveOp: Future[Int] = ???
def veryExpensiveOp(i: Int): Future[Int] = ???
for {
v1 <- expensiveOp
v2 <- veryExpensiveOp(v1)
} yield v2
*/
func main() {
result := make(chan int,1)
fmt.Println("before goroutine")
go func(c chan int) {
v1 := <- expensiveOp()
v2 := <- veryExpensiveOp(v1)
c <- v2
}(result)
fmt.Println("after goroutine")
select {
case r := <- result:
fmt.Println("inside select:",r)
break
default:
fmt.Println("default")
}
fmt.Println("About to read the result")
fmt.Println("Result = ", <-result)
fmt.Println("Blocked by above channel read")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment