title: Computing the Content Length of a Multipart form Post
body: <p>I'm trying to upload a file without loading it into memory, as shown below. Services like S3 need a <code>Content-Length</code> set in such cases. Is there a go-lang built-in to do that, or do I have to compute it myself.</p>
  
  <pre><code>package main
  
  import (
      "io"
      "mime/multipart"
      "net/http"
      "os"
      "path/filepath"
  )
  
  func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, chan error, error) {
      file, err := os.Open(path)
      if err != nil {
          return nil, nil, err
      }
      bodyReader, bodyWriter := io.Pipe()
      multiWriter := multipart.NewWriter(bodyWriter)
      errChan := make(chan error, 1)
      go func() {
          defer bodyWriter.Close()
          defer file.Close()
          part, err := multiWriter.CreateFormFile(paramName, filepath.Base(path))
          if err != nil {
              errChan &lt;- err
              return
          }
          if _, err := io.Copy(part, file); err != nil {
              errChan &lt;- err
              return
          }
          for k, v := range params {
              if err := multiWriter.WriteField(k, v); err != nil {
                  errChan &lt;- err
                  return
              }
          }
          errChan &lt;- multiWriter.Close()
      }()
      req, err := http.NewRequest("POST", uri, bodyReader)
      return req, errChan, err
  }
  </code></pre>
  
  <p>Any help would be much appreciated.</p>