go 斐波那契和管道,goroutine
package main
import "fmt"
func fibonacci(n int) int {
if n <= 2 {
return 1
}
return fibonacci(n - 1) + fibonacci(n - 2)
}
func fibonacciCalc(n int, pipe chan int) {
a := fibonacci(n)
pipe <- a
fmt.Printf("长度%d,数值%d\n",len(pipe),a)
}
func main() {
pipe := make(chan int, 10)
for i := 1; i <= 100; i++ {
go fibonacciCalc(4, pipe)
fmt.Printf("----------fibonacciCalc>>%d--len(pipe)=%d------%d----------\n",i,len(pipe),cap(pipe))
}
fmt.Println("--------------------上面执行完毕了------------------------------------------")
for i := 1; i <= 100; i++ {
fmt.Println(<- pipe)
fmt.Printf("--------- <- pipe >>%d--------len(pipe)=%d------%d----------\n",i,len(pipe),cap(pipe))
}
}
写入自己的博客中才能记得长久