Created
March 23, 2014 00:45
-
-
Save hkarim/9716664 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
) | |
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