Skip to content

Instantly share code, notes, and snippets.

@pkazmierczak
Created March 3, 2016 17:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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
}
}
@fabulousduck
Copy link

could this be done without globally defining c ?
with something like a pointer to the channel ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment