Created
July 23, 2018 01:18
-
-
Save bwangelme/2ae01b01a6825475ebe221b5971581c5 to your computer and use it in GitHub Desktop.
Go fib channel to Learn
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 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