title: Go error handling, type assertion, and the net package
body: <p>I'm learning go and trying to understand how to get more detailed error information out of the generic error type. The example I'll use is from the net package, specifically the <a href="http://golang.org/pkg/net/#DialTimeout" rel="noreferrer">DialTimeout function</a>.</p>
  
  <p>The signature is </p>
  
  <p><code>func DialTimeout(network, address string, timeout time.Duration) (Conn, error)</code></p>
  
  <p>The <a href="http://golang.org/pkg/builtin/#error" rel="noreferrer">error</a> type only defines an <code>Error() string</code> function. If I want to find out exactly why DialTimeout failed, how can I get that information? I found out that I can use type assertion to get the <code>net.Error</code> specific error:</p>
  
  <pre><code>con, err := net.DialTimeout("tcp", net.JoinHostPort(address, "22"),
      time.Duration(5) * time.Second)
  if err != nil {
      netErr, ok := err.(net.Error)
      if ok &amp;&amp; netErr.Timeout() {
          // ...
      } 
  } 
  </code></pre>
  
  <p>but that only tells me whether or not I had a timeout. For example, say I wanted to differentiate between a refused connection and no route to host. How can I do that? </p>
  
  <p>Maybe DialTimeout is too high-level to give me that kind of detail, but even looking at <a href="http://golang.org/pkg/syscall/#Connect" rel="noreferrer">syscall.Connect</a>, I don't see how to get the specific error. It just says it returns the generic error type. Compare that to the Posix <a href="http://linux.die.net/man/3/connect" rel="noreferrer">connect</a>, which will let me know why it failed with the various return codes. </p>
  
  <p>My general question is: how am I supposed to pull out error details from the generic <code>error</code> type if the golang docs don't tell me what type of errors may be returned? </p>