golang动画等待计算菲波那契结果
一个小玩意,main goroutine将计算菲波那契数列的第45个元素值。由于计算函数使用低效的递归,所以会运行相当长时间,在此期间我们想让用户看到一个可见的标识来表明程序依然在正常运行,所以来做一个动画的小图标:
package main import ( "fmt" "time" ) func main() { go spinner(100 * time.Millisecond) const n = 45 fibN := fib(n) // slow fmt.Printf("\rFibonacci(%d) = %d\n", n, fibN) } func spinner(delay time.Duration) { for { for _, r := range `-\|/` { fmt.Printf("\r%c Caculating...", r) time.Sleep(delay) } } } func fib(x int) int { if x < 2 { return x } return fib(x-1) + fib(x-2) }
相关推荐
twater000 2020-02-01
小方哥哥 2020-07-09
msmysql 2020-06-26
chinawangfei 2020-06-16
kka 2020-06-09
free0day 2020-05-25
yunfeitian 2020-05-05
ericasadun 2020-04-19
aweilark 2020-03-06
Winterto0 2020-02-10
IsanaYashiro 2020-01-28
林大鹏 2020-01-24
ilovewqf 2020-01-16
狼窝 2020-01-06
Yasin 2019-12-19
狼窝 2019-12-12
fenxinzi 2019-12-06
AKATheIronHead 2010-12-27
jiangxinyu 2011-06-07