Go 每日一庫:讓等待心裏更有底
據 2020 年 Go 官方調查報告顯示,使用 Go 進行 CLI 開發排名第二。Go 愛好者們,應該也有不少用 Go 寫命令行程序的。
今天推薦一個命令行程序有用的輔助庫:控制檯進度條。
項目地址:https://github.com/cheggaaa/pb,Star 數:2.9k+。
看一個簡單的使用例子:
package main
import (
"time"
"github.com/cheggaaa/pb/v3"
)
func main() {
count := 100000
// create and start new bar
bar := pb.StartNew(count)
// start bar from 'default' template
// bar := pb.Default.Start(count)
// start bar from 'simple' template
// bar := pb.Simple.Start(count)
// start bar from 'full' template
// bar := pb.Full.Start(count)
for i := 0; i < count; i++ {
bar.Increment()
time.Sleep(time.Millisecond)
}
bar.Finish()
}
運行結果類似這樣:
> go run test.go
37158 / 100000 [================>_______________________________] 37.16% 1m11s
如果不喜歡這個簡單的樣式,可以自己進行簡單的定製。
目前該庫最新版本是 v3,因此建議這麼使用(基於 Module):
go get github.com/cheggaaa/pb/v3
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/wEal_e_aE_0eOlgGE-1Txg