Created
January 31, 2020 13:16
-
-
Save ptflp/29dbb102c045d4e74d614b4dc3e40464 to your computer and use it in GitHub Desktop.
Golang Fibonacci on channels
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
package main | |
import "fmt" | |
func fibonacci(out chan uint, quit chan struct{}) { | |
var x, y uint | |
x, y = 0, 1 | |
for { | |
select { | |
case out <- x: | |
x, y = y, x+y | |
case <-quit: | |
fmt.Println("quit") | |
return | |
} | |
} | |
} | |
func main(){ | |
out := make(chan uint) | |
quit := make(chan struct{}) | |
go func() { | |
for i := 0; i < 100; i++ { | |
fmt.Println(<-out) | |
} | |
quit <- struct{}{} | |
}() | |
fibonacci(out, quit) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment