Skip to content

Instantly share code, notes, and snippets.

@lkumarjain
Created December 28, 2020 09:16
package main
import "fmt"
type A struct {
Name string
}
func (a *A) Go() {
fmt.Printf("GO: %v\n", a)
}
func Test(fn func()) {
fn()
}
func main() {
aa := &A{Name: "FOO"}
bb := (*A)(nil)
cc := &A{}
Test(aa.Go)
// The variable bb is nil so calling bb.Go() method is expected to cause a panic
// "runtime error: invalid memory address or nil pointer dereference",
// but the method call succeeds. Why doesn't this panic?
Test(bb.Go)
Test(cc.Go)
}
GO: &{FOO}
GO: <nil>
GO: &{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment