title: Why does reflecting the name (or package path) of the error type cause a panic in Go?
body: <p>Using reflection to get the name or package path of the <code>error</code> type in Golang causes the program to panic (with <code>panic: runtime error: invalid memory address or nil pointer dereference</code>).</p>
  
  <p>What is the reasoning for this behaviour? (Doing the same operation for other built in types returns the type name and an empty string for the package path.)</p>
  
  <p>I'm interested in terms of the design of the language - I can't figure out why it's desirable for this to behave differently for <code>error</code> types over other built in types.</p>
  
  <p>E.g:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "reflect"
  )
  
  func main() {
      var str string
      strType := reflect.TypeOf(str)
      fmt.Println(strType.Name()) // OK
      fmt.Println(strType.PkgPath())  // OK
  
      var err error
      errType := reflect.TypeOf(err)
      fmt.Println(errType.Name()) // panics
      fmt.Println(errType.PkgPath())  // also panics
  }
  </code></pre>
  
  <p>Go playground here: <a href="https://play.golang.org/p/JBMhMkjGPEV" rel="nofollow noreferrer">https://play.golang.org/p/JBMhMkjGPEV</a></p>