Ali*_*Ali 2 go progress-bar

我正在尝试使用golang进行文件下载.

我正在下载文件,没关系.在我使用cheggaaa的进度条库之后.但我不能动态.

我该怎么做动态进度条?

我的代码如下:

包主

import (
    "flag"
    "fmt"
    "io"
    "net/http"
    "net/url"
    "os"
    "strings"
    "github.com/cheggaaa/pb"
    "time"
)

/*
    usage = usage text
    version = current number
    help use Sprintf
    *cliUrl from cmd
    *cliVersion from cmd
    *cliHelp * from cmd
*/
var (
    usage      = "Usage: ./gofret -url=http://some/do.zip"
    version    = "Version: 0.1"
    help       = fmt.Sprintf("\n\n  %s\n\n\n  %s", usage, version)
    cliUrl     *string
    cliVersion *bool
    cliHelp    *bool
)

func init() {
    /*
        if *cliUrl != "" {
            fmt.Println(*cliUrl)
        }

        ./gofret -url=http://somesite.com/somefile.zip
        ./gofret -url=https://github.com/aligoren/syspy/archive/master.zip
    */
    cliUrl = flag.String("url", "", usage)

    /*
        else if *cliVersion{
            fmt.Println(flag.Lookup("version").Usage)
        }

        ./gofret -version
    */
    cliVersion = flag.Bool("version", false, version)

    /*
        if *cliHelp {
            fmt.Println(flag.Lookup("help").Usage)
        }

        ./gofret -help
    */
    cliHelp = flag.Bool("help", false, help)
}

func main() {

    /*
        Parse all flags
    */
    flag.Parse()

    if *cliUrl != "" {
        fmt.Println("Downloading file")

        /* parse url from *cliUrl */
        fileUrl, err := url.Parse(*cliUrl)

        if err != nil {
            panic(err)
        }

        /* get path from *cliUrl */
        filePath := fileUrl.Path

        /*
            seperate file.
            http://+site.com/+(file.zip)
        */
        segments := strings.Split(filePath, "/")

        /*
            file.zip filename lenth -1
        */
        fileName := segments[len(segments)-1]

        /*
            Create new file.
            Filename from fileName variable
        */
        file, err := os.Create(fileName)

        if err != nil {
            fmt.Println(err)
            panic(err)
        }
        defer file.Close()

        /*
            check status and CheckRedirect
        */
        checkStatus := http.Client{
            CheckRedirect: func(r *http.Request, via []*http.Request) error {
                r.URL.Opaque = r.URL.Path
                return nil
            },
        }

        /*
            Get Response: 200 OK?
        */
        response, err := checkStatus.Get(*cliUrl)

        if err != nil {
            fmt.Println(err)
            panic(err)
        }
        defer response.Body.Close()
        fmt.Println(response.Status) // Example: 200 OK

        /*
            fileSize example: 12572 bytes
        */
        fileSize, err := io.Copy(file, response.Body)
        /*
            progressbar worked after download :(
        */
        var countSize int = int(fileSize/1000)
        count := countSize
        bar := pb.StartNew(count)
        for i := 0; i < count; i++ {
            bar.Increment()
            time.Sleep(time.Millisecond)
        }
        bar.FinishPrint("The End!")

        if err != nil {
            panic(err)
        }


        fmt.Printf("%s with %v bytes downloaded", fileName, count)

    } else if *cliVersion {
        /*
            lookup version flag's usage text
        */
        fmt.Println(flag.Lookup("version").Usage)
    } else if *cliHelp {
        /*
            lookup help flag's usage text
        */
        fmt.Println(flag.Lookup("help").Usage)
    } else {
        /*
            using help's usage text for handling other status
        */
        fmt.Println(flag.Lookup("help").Usage)
    }
}

当我的程序运行时:

Downloading file
200 OK

下载工作进度条后:

6612 / 6612 [=====================================================] 100.00 % 7s
The End!
master.zip with 6612 bytes downloaded

我的进度条代码如下:

/*
    progressbar worked after download :(
*/
var countSize int = int(fileSize/1000)
count := countSize
bar := pb.StartNew(count)
for i := 0; i < count; i++ {
    bar.Increment()
    time.Sleep(time.Millisecond)
}
bar.FinishPrint("The End!")

如何解决进度条问题?