Ocultando valores nulos, entendiendo por qué el golang falla aquí
No entiendo cómo asegurar correctamente que algo no estánil
en este caso:
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()
}
}
La única forma en que encontré comprobar si ese valor es realmentenil
es mediante el uso de la reflexión.
¿Es este comportamiento realmente deseado? ¿O no veo algún error importante en mi código?