當我偶然發現這種明顯的不一致時,我正在做一個簡單的鏈接列表界面來了解Go界面。 nextT
始終爲零,但next()
的返回值不是。指針接收器不一致(Go bug?)
package main
import (
"fmt"
)
type LinkedList interface {
next() LinkedList
}
type T struct {
nextT *T
}
func (t *T) next() LinkedList {
//uncomment to see the difference
/*if t.nextT == nil {
return nil
}*/
return t.nextT//this is nil!
}
func main() {
t := new(T)
fmt.Println(t.nextT == nil)
var ll LinkedList
ll = t
fmt.Println(ll.next() == nil)//why isn't this nil?
}
在
next()
沒有零檢查(我不應該這樣做),我得到
true
false
有了它,我得到預期的結果
true
true
有我發現了一個錯誤還是出於某種原因,這是意外的驚喜?使用zip安裝(無MSI)在Go版本1上運行