Created
December 13, 2014 12:28
-
-
Save satoshun/3dc1302dbc163c9a9245 to your computer and use it in GitHub Desktop.
go: nil_receiver
This file contains hidden or 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 Object struct { | |
} | |
func (o Object) value() { | |
log.Println(o, "call value receiver") | |
} | |
func (o *Object) pointer() { | |
log.Println(o, "call pointer receiver") | |
} | |
func main() { | |
o := Object{} | |
o.value() | |
o.pointer() | |
p := &Object{} | |
p.value() | |
p.pointer() | |
p = nil | |
// p.value() // panic: runtime error: invalid memory address or nil pointer dereference | |
p.pointer() // not panic! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment