我写了一些代码,实际上就是这样做的:
package main
import "fmt"
type SomeInterface interface {
Retrieve(identifier string)
}
type SomeStruct struct {}
func (r SomeStruct) Retrieve(identifier string) {
fmt.Println("identifier ", identifier)
}
type Handler struct {
Name string
SomeObject SomeInterface
}
func main() {
var someStruct *SomeStruct
var h = Handler{
Name: "helo",
SomeObject: someStruct,
}
fmt.Printf("before %+v\r\n", h.SomeObject)
if h.SomeObject == nil {
fmt.Printf("during %+v\r\n", h.SomeObject)
}
fmt.Printf("after %+v\r\n", h.SomeObject)
}
请有人向我解释为什么上面的输出是:
before <nil>
after <nil>
我一直在阅读有关类型的接口的信息,但在这种情况下,我将接口分配给了一个尚未分配的指针,因此我本来以为该接口并且我会看到 - 唉,事实并非如此。nil== nilduring <nil>
凤凰求蛊
眼眸繁星
aluckdog
相关分类