Skip to content

Instantly share code, notes, and snippets.

@ligustah
Created May 5, 2015 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ligustah/de5dc3e5882877978954 to your computer and use it in GitHub Desktop.
Save ligustah/de5dc3e5882877978954 to your computer and use it in GitHub Desktop.
Weird behavior around value method errors
package main
import (
"log"
)
type ErrorType struct {
}
func (e ErrorType) Error() string {
return "error"
}
func getNilError() *ErrorType {
return nil
}
func main() {
var err error
err = getNilError()
log.Printf("Pointer: %p", err)
if err != nil {
log.Println("Received:", err.Error())
} else {
log.Println("All fine")
}
}
2015/05/05 13:07:31 Pointer: 0x0
panic: value method main.ErrorType.Error called using nil *ErrorType pointer
goroutine 1 [running]:
main.(*ErrorType).Error(0x0, 0xb, 0xc20802df48)
<autogenerated>:1 +0x82
main.main()
[ ..REDACTED.. ]/main.go:24 +0x159
goroutine 2 [runnable]:
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:90
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
goroutine 3 [runnable]:
runtime.bgsweep()
/usr/local/go/src/runtime/mgc0.go:82
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
goroutine 4 [runnable]:
runtime.runfinq()
/usr/local/go/src/runtime/malloc.go:712
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment