title: Should a Go package ever use log.Fatal and when?
body: <p>I have so far avoided use of <code>log.Fatal</code>, but I recently co-incidentally discovered these questions; <a href="https://stackoverflow.com/questions/33873305/how-to-get-100-code-coverage-in-golang">code-coverage</a> and <a href="https://stackoverflow.com/questions/30688554/how-to-test-go-function-containing-log-fatal">tests-using-log-fatal</a>.  </p>
  
  <p>One of the comments from the 100 code coverage questions says: </p>
  
  <blockquote>
    <p>... In the vast majority of cases <code>log.Fatal</code> should be only be used in main, or init functions (or possibly some things meant to be called only directly from them)"</p>
  </blockquote>
  
  <p>It go me thinking, so I began to look at the standard library code provided with Go.  There are lots of examples where the <strong>test</strong> code in the library makes use of <code>log.Fatal</code> which seems fine.  There are a few examples outside of the test code, such as in <a href="https://golang.org/src/net/http/transport.go?h=log.Fatal#L408" rel="noreferrer"><code>net/http</code></a>, shown below:</p>
  
  <pre><code>// net/http/transport.go 
  func (t *Transport) putIdleConn(pconn *persistConn) bool {
      ...
      for _, exist := range t.idleConn[key] {
          if exist == pconn {
              log.Fatalf("dup idle pconn %p in freelist", pconn)
          }
      }
      ...
  }
  </code></pre>
  
  <p>If its is best practice to avoid use of <code>log.Fatal</code>, why is it used at all in the standard libraries, I would have expected just return an error.  It seems unfair to the user of the library to cause <code>os.Exit</code> to be called and not providing any chance for the application to clean-up.</p>
  
  <p>I may be naive, so hence my question as a better practice would seem to be to call <code>log.Panic</code> which can be recovered and my theoretical long running stable application might have a chance of rising from the ashes.</p>
  
  <p>So what would best-practise say for Go about when should log.Fatal should be used?</p>