看一看https://play.golang.org/p/xtRtk1T_ZW(代码转载如下):
func main() {
// outputMax is the number of progress items to print, excluding the 100% completion item.
// There will always be at least 2 items output: 0% and 100%.
outputMax := 10
for lineLen := 1; lineLen < 200; lineLen++ {
fmt.Printf("lineLen=%-3d ", lineLen)
printProgress(lineLen, outputMax)
}
}
// Calculate the current progress.
func progress(current, max int) int {
return 100 * current/max
}
// Calculate the number of items in a group.
func groupItems(total, limit int) int {
v := total/limit
if total%limit != 0 {
v++
}
return v
}
// Print the progress bar.
func printProgress(lineLen, outputMax int) {
itemsPerGroup := groupItems(lineLen, outputMax)
for i := 0; i < lineLen; i++ {
if i%itemsPerGroup == 0 {
fmt.Printf("--%d%%--", progress(i, lineLen))
}
}
fmt.Println("--100%--")
}
如果你愿意,你可以使用https://play.golang.org/p/aR6coeLhAk看到执行过的outputMax
和lineLen
各种值循环您喜欢的值为outputMax
(8 <= outputMax < 13
最适合我)。进度条的输出在默认情况下是禁用的,但您可以在main
中轻松启用它。
只是因为你不想花时间建立一个强大的进度条..它已经完成很好 https://github.com/cheggaaa/pb – reticentroot
我会建议使用现有的库以及。没有理由重新发明轮子。 –