Created
November 1, 2015 10:52
-
-
Save paulobsf/995481f45fc925cb697a to your computer and use it in GitHub Desktop.
Go-Routine Fibonacci sequence
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
// from: http://jbu.io/2015/10/28/six-reasons-why-i-love-go/ | |
package main | |
import "fmt" | |
func fibonacci() chan int { | |
c := make(chan int) | |
go func() { | |
for i, j := 0, 1; ; i, j = i+j,i { | |
c <- i | |
} | |
}() | |
return c | |
} | |
func main() { | |
c := fibonacci() | |
for n := 0; n < 12 ; n++ { | |
fmt.Printf("%d ", <- c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment