Skip to content

Instantly share code, notes, and snippets.

@bwangelme
Created July 23, 2018 01:18
Show Gist options
  • Save bwangelme/2ae01b01a6825475ebe221b5971581c5 to your computer and use it in GitHub Desktop.
Save bwangelme/2ae01b01a6825475ebe221b5971581c5 to your computer and use it in GitHub Desktop.
Go fib channel to Learn
package main
import "fmt"
func dup3(in <-chan int) (<-chan int, <-chan int, <-chan int) {
a, b, c := make(chan int, 2), make(chan int, 2), make(chan int, 2)
go func() {
for {
val := <-in
a <- val
b <- val
c <- val
}
}()
return a, b, c
}
func fib() <-chan int {
x := make(chan int, 2)
a, b, out := dup3(x)
go func() {
x <- 0
x <- 1
<-a
for {
x <- <-a + <-b
}
}()
return out
}
func main() {
x := fib()
for i := 0; i < 10; i++ {
fmt.Println(<-x)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment