code: func (bp *BytesPipe) Write(p []byte) (int, error) {
  	bp.mu.Lock()
  
  	written := 0
  loop0:
  	for {
  		if bp.closeErr != nil {
  			bp.mu.Unlock()
  			return written, ErrClosed
  		}
  
  		if len(bp.buf) == 0 {
  			bp.buf = append(bp.buf, getBuffer(64))
  		}
  		// get the last buffer
  		b := bp.buf[len(bp.buf)-1]
  
  		n, err := b.Write(p)
  		written += n
  		bp.bufLen += n
  
  		// errBufferFull is an error we expect to get if the buffer is full
  		if err != nil && err != errBufferFull {
  			bp.wait.Broadcast()
  			bp.mu.Unlock()
  			return written, err
  		}
  
  		// if there was enough room to write all then break
  		if len(p) == n {
  			break
  		}
  
  		// more data: write to the next slice
  		p = p[n:]
  
  		// make sure the buffer doesn't grow too big from this write
  		for bp.bufLen >= blockThreshold {
  			bp.wait.Wait()
  			if bp.closeErr != nil {
  				continue loop0
  			}
  		}
  
  		// add new byte slice to the buffers slice and continue writing
  		nextCap := b.Cap() * 2
  		if nextCap > maxCap {
  			nextCap = maxCap
  		}
  		bp.buf = append(bp.buf, getBuffer(nextCap))
  	}
  	bp.wait.Broadcast()
  	bp.mu.Unlock()
  	return written, nil
  }
docs: // Write writes p to BytesPipe.
  // It can allocate new []byte slices in a process of writing.