为什么Go允许我调用未实现的方法?

Kev*_*uth 4 go

Go似乎没有强制遵守接口的结构.为什么以下代码编译?

package main

type LocalInterface interface {
    SomeMethod(string) error
    SomeOtherMethod(string) error
}

type LocalStruct struct {
    LocalInterface
    myOwnField string
}

func main() {
    var localInterface LocalInterface = &LocalStruct{myOwnField:"test"}

    localInterface.SomeMethod("calling some method")
}
Run Code Online (Sandbox Code Playgroud)

似乎这不应该编译,因为SomeMethod没有实现.go build结果没有问题.

运行它会导致运行时错误:

> go run main.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x4013b0]

goroutine 1 [running]:
panic(0x460760, 0xc08200a090)
        C:/Go/src/runtime/panic.go:464 +0x3f4
main.(*LocalStruct).SomeMethod(0xc0820064e0, 0x47bf30, 0x13, 0x0, 0x0)
        <autogenerated>:3 +0x70
main.main()
        C:/Users/kdeenanauth/Documents/git/go/src/gitlab.com/kdeenanauth/structTest/main.go:16 +0x98
exit status 2
Run Code Online (Sandbox Code Playgroud)

kos*_*tya 7

嵌入类型时(在您的示例LocalInterface中嵌入了类型LocalStruct),Go会创建嵌入类型的字段并将其方法提升为封闭类型.

所以下面的声明

type LocalStruct struct {
    LocalInterface
    myOwnField string
}
Run Code Online (Sandbox Code Playgroud)

相当于

type LocalStruct struct {
    LocalInterface LocalInterface
    myOwnField string
}

func (ls *LocalStruct) SomeMethod(s string) error {
    return ls.LocalInterface.SomeMethod(s)
}
Run Code Online (Sandbox Code Playgroud)

您的程序会因为LocalInterface字段为空而使用nil指针取消引用nil.

以下程序"修复"恐慌(http://play.golang.org/p/Oc3Mfn6LaL):

package main

type LocalInterface interface {
    SomeMethod(string) error
}

type LocalStruct struct {
     LocalInterface
    myOwnField string
}

type A int

func (a A) SomeMethod(s string) error {
    println(s)
    return nil
}

func main() {
    var localInterface LocalInterface = &LocalStruct{
        LocalInterface: A(10),
        myOwnField:     "test",
    }

    localInterface.SomeMethod("calling some method")
}
Run Code Online (Sandbox Code Playgroud)