为什么我的上传进度栏在传输完成之前完成?

I'm sending a POST request to an API, and using a third-party library (https://github.com/cheggaaa/pb), but my upload progress bar is going to done before the file transfer actually completes.

package main

import(
    pb "gopkg.in/cheggaaa/pb.v1"
    "net/http"
)

func main() {
    file, e := os.Open(path)
    if e != nil {
        log.Fatal()
    }
    defer file.Close()

    bar := pb.New(int(fi.Size()))
    bar.Start()


    req, err := http.NewRequest("POST", url, body)

    resp, err := client.Do(req)

    bar.Finish()

    return
}

It starts at

12.64 MB / 12.64 MB [======================] 100.00% 12.59 MB/s 0s

And at completion goes to:

12.64 MB / 12.64 MB [======================] 100.00% 626.67 KB/s 20s

Is this because the HTTP Req handler is reading the file into memory and incrementing the progress bar? What am I missing?

I checked out this post, (Go: Tracking POST request progress), but I don't see how that is different from the library I'm using. I've previously tried using io.Copy to a buffer with the progress bar reader reading from that, but as soon as the Request is sent it does the same thing.

As I wrote in the comment, your question doesn't contain all required information, but here is the sample app that posts the file as multipart-form to the remote server with the progress bar:

package main

import (
    "github.com/cheggaaa/pb"
    "os"
    "time"
    "bytes"
    "mime/multipart"
    "io"
    "io/ioutil"
    "net/http"
)

func main() {
    body := &bytes.Buffer{}
    bodyWriter := multipart.NewWriter(body)

    fw, _ := bodyWriter.CreateFormFile("file", "filename.jpg")
    fh, _ := os.Open("filename.jpg")

    io.Copy(fw, fh)
    bodyWriter.Close()

    bar := pb.New(body.Len()).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 10)
    bar.ShowSpeed = true
    bar.Start()

    reader := bar.NewProxyReader(body)

    resp, _ := http.Post("http://domain.com", bodyWriter.FormDataContentType(), reader)
    defer resp.Body.Close()

    response, _ := ioutil.ReadAll(resp.Body)
    print(string(response))
}

Got it, turns out another package was causing issues with the HTTP Request. The code below works perfectly.

package main

import(
    pb "gopkg.in/cheggaaa/pb.v1"
    "fmt"
    "os"
    "log"
    "io/ioutil"
    "net/http"
)

func main() {
    path := "/Users/me/testfile.txt"
    file, e := os.Open(path)
    if e != nil {
        log.Fatal("File Error")
    }
    defer file.Close()
    fi, e := file.Stat()
    if e != nil {
      log.Fatal("File Stat Error")
    }

    bar := pb.New(int(fi.Size()))
    bar.Start()

    client := &http.Client{}
    req, e := http.NewRequest("POST", "http://posttestserver.com/post.php", bar.NewProxyReader(file))
    if e != nil {
      log.Fatal("Request Error")
    }
    resp, e := client.Do(req)
    if e != nil {
      log.Fatal("Response Error")
    }
    bar.Finish()
    respBody, e := ioutil.ReadAll(resp.Body)
    fmt.Println(string(respBody))

    return
}

Thanks to http://posttestserver.com for giving me somewhere to easily troubleshoot!