mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-13 15:12:32 +00:00
good good study, day day up!
This commit is contained in:
38
vendor/gopl.io/ch8/spinner/main.go
generated
vendored
Normal file
38
vendor/gopl.io/ch8/spinner/main.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 218.
|
||||
|
||||
// Spinner displays an animation while computing the 45th Fibonacci number.
|
||||
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", r)
|
||||
time.Sleep(delay)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fib(x int) int {
|
||||
if x < 2 {
|
||||
return x
|
||||
}
|
||||
return fib(x-1) + fib(x-2)
|
||||
}
|
||||
|
||||
//!-
|
Reference in New Issue
Block a user