[golang]golang如何覆盖输出console,实现进度条

 

 

 

package main

import(
        "fmt"
        "os"
        "time"
)

func main(){
        for i :=0;i!=10;i=i+1{
                fmt.Fprintf(os.Stdout,"result is %d\r",i)
                time.Sleep(time.Second*1)
        }
        fmt.Println("Over")
}

 golang一个骚气的进度提示库

spinner is a simple package to add a spinner / progress indicator to any terminal application. Examples can be found below as well as full examples in the examples directory.

For more detail about the library and its features, reference your local godoc once installed.

Contributions welcome!

安装

go get -u -v github.com/briandowns/spinner

下面是效果图

(Numbered by their slice index)

←↖↑↗→↘↓↙▁▃▄▅▆▇█▇▆▅▄▃▁▖▘▝▗┤┘┴└├┌┬┐◢◣◤◥◰◳◲◱◴◷◶◵◐◓◑◒.oO@*◡◡⊙⊙◠◠⣾⣽⣻⢿⡿⣟⣯⣷>))'> >))'> >))'> >))'> >))'> <'((< <'((< <'((<⠁⠂⠄⡀⢀⠠⠐⠈⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏abcdefghijklmnopqrstuvwxyz▉▊▋▌▍▎▏▎▍▌▋▊▉■□▪▫←↑→↓╫╪⇐⇖⇑⇗⇒⇘⇓⇙⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈⠈⠉⠋⠓⠒⠐⠐⠒⠖⠦⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠴⠲⠒⠂⠂⠒⠚⠙⠉⠁⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋ヲァィゥェォャュョッアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン. .. ...▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▏▎▍▌▋▊▉█▇▆▅▄▃▂▁.oO°Oo.+xv<^>>>---> >>---> >>---> >>---> >>---> <---<< <---<< <---<< <---<< <---<<[] [=] [==] [===] [====] [=====] [======] [=======] [========] [=========] [==========](*---------) (-*--------) (--*-------) (---*------) (----*-----) (-----*----) (------*---) (-------*--) (--------*-) (---------*)█▒▒▒▒▒▒▒▒▒ ███▒▒▒▒▒▒▒ █████▒▒▒▒▒ ███████▒▒▒ ██████████[ ] [=> ] [===> ] [=====> ] [======> ] [========> ] [==========> ] [============> ] [==============> ] [================> ] [==================> ] [===================>]🕐 🕑 🕒 🕓 🕔 🕕 🕖 🕗 🕘 🕙 🕚 🕛🕐 🕜 🕑 🕝 🕒 🕞 🕓 🕟 🕔 🕠 🕕 🕡 🕖 🕢 🕗 🕣 🕘 🕤 🕙 🕥 🕚 🕦 🕛 🕧🌍 🌎 🌏◜ ◝ ◞ ◟⬒ ⬔ ⬓ ⬕⬖ ⬘ ⬗ ⬙[>>> >] []>>>> [] [] >>>> [] [] >>>> [] [] >>>> [] [] >>>>[] [>> >>]

示例

package main

import (
    "github.com/briandowns/spinner"
    "time"
)

func main() {
    s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)  // Build our new spinner
    s.Start()                                                    // Start the spinner
    time.Sleep(4 * time.Second)                                  // Run for some time to simulate work
    s.Stop()
}

更多内容请看github