Original post

Hi! I’m trying to learn , by taking my small projects from other languages, and rewriting them in .

I’ve found my python fibonacci sequence algorithm, that calculates n-th number in that sequence, and realized it could be much faster, if it was calculating f(x-1) and f(x-2) asynchronously. But i get deadlock, no matter how small my input is :/

Can someone explain to me, what’s wrong? Or at least fix the code, so I can analyse and learn!

Thanks in advance!

package main import "fmt" func fib(x int, res chan int){ if (x == 0){ res <- 0 } if (x == 1){ res <- 1 } if (x != 1 && x != 0){ a := make(chan int) b := make(chan int) go fib(x-1,a) go fib(x-2,b) res <- ((<-a)+(<-b)) } } func main(){ result := make(chan int) fib(2,result) fmt.Println(<-result) } 

submitted by /u/MadBananaEater
[link] [comments]