*Factorial Program in GoLang. Encountering deadlock.*

*Hello All,*

I am pretty new to GoLang , so please excuse me if question/doubt is silly. 

I am trying to write a Factorial program of numbers ranging from 0 to 5. My 
goal is to launch separate GO routines for each number and then collect o/p 
and print it. I have tries writing code which can be seen 
@ https://play.golang.org/p/MTzTuroxE5 (also below) .. but i am 
encountering DEADLOCK error. I have been trying to synchronize it but i 
have failed to do it so far. Please help me in understanding that how else 
i can write this program so that i properly close sending channel to avoid 
error.

--
package main

import (
"fmt"
"sync"
)

var wg sync.WaitGroup

func factorial(x int) int {
if x > 0 {
return x*factorial(x-1)
}
return 1
}

func fact_worker(fact_resp_chan chan string, x int) {
defer wg.Done()
response := factorial(x)
fact_resp_chan <-  fmt.Sprintf("Factorial of %d is : %d", x, response)
}


func main() {
fact_chan := make(chan string)

for n := 0; n <=5 ; n++ {
wg.Add(1)
go fact_worker(fact_chan, n)
}



for resp := range(fact_chan) {
fmt.Printf("%s\n", resp)
}
wg.Wait()

}
--

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to