Skip to content

Instantly share code, notes, and snippets.

@pkazmierczak
Created March 3, 2016 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkazmierczak/3a6cecce5581b530b2d7 to your computer and use it in GitHub Desktop.
Save pkazmierczak/3a6cecce5581b530b2d7 to your computer and use it in GitHub Desktop.
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