为什么 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")

}

似乎这不应该编译,因为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


慕尼黑8549860
浏览 226回答 2
2回答

森栏

当一个类型被嵌入(在你的例子中LocalInterface被嵌入在里面LocalStruct)时,Go 会创建一个嵌入类型的字段并将其方法提升为封闭类型。所以下面的声明type LocalStruct struct {&nbsp; &nbsp; LocalInterface&nbsp; &nbsp; myOwnField string}相当于type LocalStruct struct {&nbsp; &nbsp; LocalInterface LocalInterface&nbsp; &nbsp; myOwnField string}func (ls *LocalStruct) SomeMethod(s string) error {&nbsp; &nbsp; return ls.LocalInterface.SomeMethod(s)}您的程序因 nil 指针取消引用而恐慌,因为LocalInterfacefield 是nil.以下程序“修复”了恐慌(http://play.golang.org/p/Oc3Mfn6LaL):package maintype LocalInterface interface {&nbsp; &nbsp; SomeMethod(string) error}type LocalStruct struct {&nbsp; &nbsp; &nbsp;LocalInterface&nbsp; &nbsp; myOwnField string}type A intfunc (a A) SomeMethod(s string) error {&nbsp; &nbsp; println(s)&nbsp; &nbsp; return nil}func main() {&nbsp; &nbsp; var localInterface LocalInterface = &LocalStruct{&nbsp; &nbsp; &nbsp; &nbsp; LocalInterface: A(10),&nbsp; &nbsp; &nbsp; &nbsp; myOwnField:&nbsp; &nbsp; &nbsp;"test",&nbsp; &nbsp; }&nbsp; &nbsp; localInterface.SomeMethod("calling some method")}

万千封印

经过进一步调查,我发现避免嵌入得到了适当的错误处理处理。在我的情况下,这将是首选:package maintype LocalInterface interface {&nbsp; &nbsp; SomeMethod(string) error&nbsp; &nbsp; SomeOtherMethod(string) error}type LocalStruct struct {&nbsp; &nbsp; myOwnField string}func main() {&nbsp; &nbsp; var localInterface LocalInterface = &LocalStruct{myOwnField:"test"}&nbsp; &nbsp; localInterface.SomeMethod("calling some method")}结果是:.\main.go:13:不能在赋值中使用 LocalStruct 文字(类型 *LocalStruct)作为 LocalInterface 类型:*LocalStruct 没有实现 LocalInterface(缺少 SomeMethod 方法)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go