隱藏零值,理解金剛在這里失敗的原因我不明白如何正確地保證某件事不是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()
}}我找到的唯一方法是檢查這個(gè)值是否真的nil是通過反射。這真的是想要的行為嗎?還是我沒有看到代碼中的一些重大錯(cuò)誤?在這里播放鏈接
隱藏零值,理解金剛在這里失敗的原因
慕運(yùn)維8079593
2019-07-12 10:26:52