title: Golang io.copy twice on the request body
body: <p>I am building a blob storage system and i picked Go as the programming language.
  I create a stream to do a multipart file upload from client to the blob server.</p>
  
  <p>The stream works fine, but i want to make a sha1 hash from the request body. I need to io.Copy the body twice. The sha1 gets created but the multipart streams 0 bytes after that.</p>
  
  <ol>
  <li>For creating the hash</li>
  <li>For streaming the body as multipart</li>
  </ol>
  
  <p>any idea how i can do this?</p>
  
  <p>the client upload</p>
  
  <pre><code>func (c *Client) Upload(h *UploadHandle) (*PutResult, error) {
  body, bodySize, err := h.Read()
  if err != nil {
      return nil, err
  }
  
  // Creating a sha1 hash from the bytes of body
  dropRef, err := drop.Sha1FromReader(body)
  if err != nil {
      return nil, err
  }
  
  bodyReader, bodyWriter := io.Pipe()
  writer := multipart.NewWriter(bodyWriter)
  
  errChan := make(chan error, 1)
  go func() {
      defer bodyWriter.Close()
      part, err := writer.CreateFormFile(dropRef, dropRef)
      if err != nil {
          errChan &lt;- err
          return
      }
      if _, err := io.Copy(part, body); err != nil {
          errChan &lt;- err
          return
      }
      if err = writer.Close(); err != nil {
          errChan &lt;- err
      }
  }()
  
  req, err := http.NewRequest("POST", c.Server+"/drops/upload", bodyReader)
  req.Header.Add("Content-Type", writer.FormDataContentType())
  resp, err := c.Do(req)
  if err != nil {
      return nil, err
  }
    .....
   }
  </code></pre>
  
  <p>the sha1 func</p>
  
  <pre><code>func Sha1FromReader(src io.Reader) (string, error) {
  hash := sha1.New()
  _, err := io.Copy(hash, src)
  if err != nil {
      return "", err
  }
  return hex.EncodeToString(hash.Sum(nil)), nil
  </code></pre>
  
  <p>}</p>
  
  <p>upload handle</p>
  
  <pre><code>func (h *UploadHandle) Read() (io.Reader, int64, error) {
  var b bytes.Buffer
  
  hw := &amp;Hasher{&amp;b, sha1.New()}
  n, err := io.Copy(hw, h.Contents)
  
  if err != nil {
      return nil, 0, err
  }
  
  return &amp;b, n, nil
  </code></pre>
  
  <p>}</p>