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))
	}
}
posted @ 2022-02-26 22:38  ty1539  阅读(31)  评论(0编辑  收藏  举报