title: Need explanation on error reported from bufio package of golang
body: <pre><code>package main
  
  import (
    &quot;bufio&quot;
    &quot;fmt&quot;
    &quot;os&quot;
  )
  
  func main() {
    writeBuf := make([]byte, 1)
  
    fp, err := os.OpenFile(&quot;test.d&quot;, os.O_CREATE, 0777)
    bf := bufio.NewWriterSize(fp, 3)
  
    writeBuf = []byte(&quot;1&quot;)
    writeCount, err := bf.Write(writeBuf)
    fmt.Println(writeCount)
  
    writeBuf = []byte{'2'}
    writeCount, err = bf.Write(writeBuf)
    fmt.Println(writeCount)
  
    writeBuf = []byte{'3'}
    writeCount, err = bf.Write(writeBuf)
    fmt.Println(writeCount)
  
    writeBuf = []byte{'4'}
    writeCount, err = bf.Write(writeBuf)
    if err != nil {
      fmt.Println(&quot;Error4:&quot;, err)
      os.Exit(-1)
    }
    fmt.Println(writeCount)
  
    bf.Flush()
    fp.Close()
  
  }
  </code></pre>
  <p>Why does this give the error:</p>
  <pre><code> 1
   1
   1
   Error4: write test.d: bad file descriptor
   exit status 255
  </code></pre>
  <p>I thought that NewWriterSize of 3 means that after three bytes are written, the output is flushed. I am getting the error on the 4th write. If I change the NewWriterSize buffer size to 4, it works. For brevity, I have omitted error checks where error is not reported.</p>