title: Return nil or custom error in Go
body: <p>When using a custom <code>Error</code> type in Go (with extra fields to capture some details), when trying to return <code>nil</code> as value of this type, I get compile errors like <code>cannot convert nil to type DetailedError</code> or like <code>cannot use nil as type DetailedError in return argument</code>, from code looking mostly like this:</p>
  
  <pre><code>type DetailedError struct {
      x, y int
  }
  
  func (e DetailedError) Error() string {
      return fmt.Sprintf("Error occured at (%s,%s)", e.x, e.y)
  }
  
  func foo(answer, x, y int) (int, DetailedError) {
      if answer == 42 {
          return 100, nil  //!! cannot use nil as type DetailedError in return argument
      }
      return 0, DetailedError{x: x, y: y}
  }
  </code></pre>
  
  <p>(full snippet: <a href="https://play.golang.org/p/4i6bmAIbRg" rel="noreferrer">https://play.golang.org/p/4i6bmAIbRg</a>)</p>
  
  <p><strong>What would be the idiomatic way to solve this problem?</strong> (Or any way that works...)</p>
  
  <p>I actually need the extra fields on errors because I have detailed error messages constructed by complex logic from simpler ones etc., and if I would just fall back to "string errors" I'd basically have to <em>parse</em> those strings to pieces and have logic happen based on them and so on, which seems really ugly (I mean, why serialize to strings info you know you're gonna need to deserialize later...)</p>