Skip to content

Panic if receiver is nil pointer #47551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
k0923 opened this issue Aug 5, 2021 · 2 comments
Closed

Panic if receiver is nil pointer #47551

k0923 opened this issue Aug 5, 2021 · 2 comments

Comments

@k0923
Copy link

k0923 commented Aug 5, 2021

What version of Go are you using (go version)?

$ go version
 1.16

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

package main

type Element interface {
Body() interface{}
}

func test(e Element) {
if e != nil {
e.Body()
}
}

type Div struct{}

func (div Div) Body() interface{} {
return "div"
}

func getDiv() *Div {
return nil
}

func main() {
// panic: value method main.Div.Body called using nil *Div pointer

// goroutine 1 [running]:
// main.(*Div).Body(0x0, 0x647ae0, 0xc00001c0b8)
//         <autogenerated>:1 +0x53
// main.test(...)
//        main.go:9
// main.main()
//         main.go:34 +0x3b
// exit status 2
test(getDiv())

}

What did you expect to see?

No panic or forbbiden to compile

What did you see instead?

Panic

@mvdan
Copy link
Member

mvdan commented Aug 5, 2021

The Go project doesn't use the issue tracker for questions. See https://golang.org/wiki/Questions.

@mvdan mvdan closed this as completed Aug 5, 2021
@D1CED
Copy link

D1CED commented Aug 5, 2021

Your error is related to https://golang.org/doc/faq#nil_error

@golang golang locked and limited conversation to collaborators Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants