title: Reading bytes into Go buffer with a fixed stride size
body: <p>I'd like to read and process 1024 bytes at a time in my file given by filename. I don't understand how to construct the outer loop correctly, especially to accommodate the final stride in which the buffer will contain fewer than 1024 bytes</p>
  
  <p>What I have tried:</p>
  
  <pre><code>fs, _ := os.Open(filename)
  defer fs.Close()
  n := 1024 // 1kb
  buff := make([]byte, n)
  for {
      buff = make([]byte, n) // is this initialized correctly?
      n1, err := fs.Read(buff)
      if err != nil {
          if err == io.EOF {
              break
          }
          fmt.Println(err)
          break
      }
      fmt.Println("read n1 bytes...", n1)
      fmt.Println(buff)
  }
  </code></pre>
  
  <p>I have seen the following resources:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/13660864/reading-specific-number-of-bytes-from-a-buffered-reader-in-golang">Reading specific number of bytes from a buffered reader in golang</a></li>
  <li><a href="https://stackoverflow.com/questions/24817494/read-arbitrary-amount-of-bytes-into-buffer-golang">Read arbitrary amount of bytes into buffer Golang</a></li>
  <li><a href="https://stackoverflow.com/questions/14514201/how-to-read-a-binary-file-in-go">How to read a binary file in Go</a></li>
  <li><a href="https://stackoverflow.com/questions/27814408/working-with-raw-bytes-from-a-network-in-go">Working with raw bytes from a network in go</a></li>
  <li><a href="https://gobyexample.com/reading-files" rel="nofollow noreferrer">https://gobyexample.com/reading-files</a></li>
  <li><a href="https://stackoverflow.com/questions/28639859/writing-into-fixed-size-buffers-in-golang-with-offsets">Writing into fixed size Buffers in Golang with offsets</a></li>
  <li><a href="https://rwinslow.com/posts/use-flatbuffers-in-golang/" rel="nofollow noreferrer">https://rwinslow.com/posts/use-flatbuffers-in-golang/</a></li>
  <li><a href="https://stackoverflow.com/questions/8757389/reading-file-line-by-line-in-go">reading file line by line in go</a></li>
  <li><a href="https://stackoverflow.com/questions/29313133/how-do-i-read-in-a-large-flat-file-in-golang">How do I read in a large flat file in Golang</a></li>
  <li><a href="https://golang.org/pkg/io/#ReadFull" rel="nofollow noreferrer">https://golang.org/pkg/io/#ReadFull</a></li>
  </ul>