Created
March 3, 2016 17:00
-
-
Save pkazmierczak/3a6cecce5581b530b2d7 to your computer and use it in GitHub Desktop.
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 = make(chan int) | |
func factorial(name string, number int) { | |
var f = 1 | |
for i := 2; i < number+1; i++ { | |
fmt.Printf("Task %s: Compute factorial(%d)...\n", name, i) | |
time.Sleep(time.Second * 1) | |
f *= i | |
} | |
fmt.Printf("Task %s completed: factorial(%d) = %d\n", name, number, f) | |
c <- f | |
} | |
func main() { | |
go factorial("A", 8) | |
go factorial("B", 3) | |
for i := 0; i < 2; i++ { | |
<-c | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could this be done without globally defining
c
?with something like a pointer to the channel ?