Golang Playground 进度条示例
Golang Playground 进度条示例
package main
import (
"fmt"
"strings"
"time"
)
func main() {
const col = 30
// Clear the screen by printing \x0c.
bar := fmt.Sprintf("\x0c[%%-%vs]", col)
for i := 0; i < col; i++ {
fmt.Printf(bar, strings.Repeat("=", i)+">")
time.Sleep(100 * time.Millisecond)
}
fmt.Printf(bar+" Done!", strings.Repeat("=", col))
}
- 符号
\x0c是
| Oct | Dec | Char | Hex | CTRL Key | Comments |
|---|---|---|---|---|---|
| \014 | 12 | FF | \x0C | ^L \f | (Form feed) |
换页符
bar := fmt.Sprintf("\x0c[%%-%vs]", col)会生成"%-30s",其中-表示左对齐,不足的部分使用空白填充,30表示需要填充的长度,\x0c起到 clear 的作用bar可以再次作为格式化的字符串使用
输出:
[======> ]
...
[============================> ]
...
[==============================] Done!

浙公网安备 33010602011771号