Skip to content

Instantly share code, notes, and snippets.

@satoshun
Created December 13, 2014 12:28
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 satoshun/3dc1302dbc163c9a9245 to your computer and use it in GitHub Desktop.
Save satoshun/3dc1302dbc163c9a9245 to your computer and use it in GitHub Desktop.
go: nil_receiver
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