Skip to content

Panic if receiver is nil pointer #47551

Closed
@k0923

Description

@k0923

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions