Skip to content

Instantly share code, notes, and snippets.

@johngrib
Created October 1, 2018 13:25
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 johngrib/c2d308d4264da9398b13cf87121127d5 to your computer and use it in GitHub Desktop.
Save johngrib/c2d308d4264da9398b13cf87121127d5 to your computer and use it in GitHub Desktop.
Fibonacci example with goroutine
package main
import "fmt"
func main() {
c := make(chan int, 3)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
quit <- 0
}()
fibonacci(c, quit)
}
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment