这个和语言关系不大,之前用C++写过,这里用Go再写一次:
Go Playgroundpackage main
import (
"fmt"
"strings"
"time"
)
func Bar(vl int, width int) string {
return fmt.Sprintf("%s%*c", strings.Repeat("█", vl/10), vl/10-width+1,
([]rune(" ▏▎▍▌▋▋▊▉█"))[vl%10])
}
func main() {
for i := 0; i <= 100; i++ {
fmt.Printf("\f%s%d%%", Bar(i, 20), i)
time.Sleep(200 * time.Millisecond)
}
}