Skip to content

Instantly share code, notes, and snippets.

@ptflp
Created January 31, 2020 13:16
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 ptflp/29dbb102c045d4e74d614b4dc3e40464 to your computer and use it in GitHub Desktop.
Save ptflp/29dbb102c045d4e74d614b4dc3e40464 to your computer and use it in GitHub Desktop.
Golang Fibonacci on channels
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