/Output Secret
Created
December 28, 2020 09:16
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 "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) | |
} |
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
GO: &{FOO} | |
GO: <nil> | |
GO: &{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment