Created
May 5, 2015 11:10
-
-
Save ligustah/de5dc3e5882877978954 to your computer and use it in GitHub Desktop.
Weird behavior around value method errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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