控制台进度条
发布者:admin 发表于:438天前 阅读数:685 评论:0

大家好,我是欧盆索思(opensource),每天为你带来优秀的开源项目!

据 2020 年 Go 官方调查报告显示,使用 Go 进行 CLI 开发排名第二。Go 爱好者们,应该也有不少用 Go 写命令行程序的。

今天推荐一个命令行程序有用的辅助库:控制台进度条。

项目地址:https://github.com/cheggaaa/pb,Star 数:2.7k+。

看一个简单的使用例子:

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