隐藏零值,理解金刚在这里失败的原因
nil
package main type shower interface { getWater() []shower}type display struct { SubDisplay *display}func (d display) getWater() []shower { return []shower{display{}, d.SubDisplay}}func main() { // SubDisplay will be initialized with null s := display{} // water := []shower{nil} water := s.getWater() for _, x := range water { if x == nil { panic("everything ok, nil found") } //first iteration display{} is not nil and will //therefore work, on the second iteration //x is nil, and getWater panics. x.getWater() }}
nil
慕莱坞森
相关分类