title: Safely close a file descriptor in golang
body: <p>Another question <a href="https://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file-using-go">How to read/write from/to file using Go?</a> got into safe closing of file descriptors in a comment.</p>
  <blockquote>
  <p>Note that these examples aren't checking the error return from
  fo.Close(). From the Linux man pages close(2): Not checking the return
  value of close() is a common but nevertheless serious programming
  error. It is quite possible that errors on a previous write(2)
  operation are first reported at the final close(). Not checking the
  return value when closing the file may lead to silent loss of data.
  This can especially be observed with NFS and with disk quota. – Nick
  Craig-Wood Jan 25 '13 at 7:12</p>
  </blockquote>
  <p>The solution that updated the post used a panic:</p>
  <pre><code>// close fo on exit and check for its returned error
  defer func() {
      if err := fo.Close(); err != nil {
          panic(err)
      }
  }()
  </code></pre>
  <p>I want to hand this error as a value instead of panicking.</p>