title: Multiple defers vs deferred anonymous function
body: <p>Is it safer or more idiomatic to issue multiple <code>defer</code> statements which are dependent on the order, or to defer an anonymous function which packages the logic?</p>
  
  <p>Examples:</p>
  
  <pre class="lang-go prettyprint-override"><code>defer os.Remove(tempFile.Name())
  defer tempFile.Close()
  </code></pre>
  
  <p>In the case above the syntax is minimal, yet the order of defers is reverse to the logic to be executed.</p>
  
  <p>In the case below there are more lines, more "syntax", but the logic is in a more natural order:</p>
  
  <pre class="lang-go prettyprint-override"><code>defer func() {
      tempFile.Close()
      os.Remove(tempFile.Name())
  }()
  </code></pre>
  
  <p>Which one to use?</p>