Escondendo valores nulos, entendendo por que o golang falha aqui
Não consigo entender como garantir corretamente que algo não estánil
nesse 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()
}
}
A única maneira que encontrei para verificar se esse valor é realmentenil
é usando reflexão.
Esse comportamento é realmente desejado? Ou não vejo algum erro grave no meu código?