Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 1
    that's a pretty... big fail isn't it? How can I write a library that provides interfaces that way? I could assure that it works correctly if the users provides correct types with nil value. I guess if x == (shower)(nil) would make sense but that is just shocking.. Commented Mar 19, 2015 at 6:52
  • 5
    There is nothing shocking about it. Your return a slice of two interface values, both are non-nil. One of those non-nil interface values contains a nil value. The interface value has to be non-nil to contain anything, even a nil. You can either fix it like icza suggested, or redesign your API, e.g. not returning a slice of interfaces. Commented Mar 19, 2015 at 7:06
  • 2
    @sharpner You can provide a library which uses/returns values of interface types. But if the value is "missing", return nil explicitly. Commented Mar 19, 2015 at 7:11
  • I'm still trying to wrap my head around how to redesign the api. Maybe a typedef something []shower will get me further or I don't know yet. But I really learned something quite new about go today. My example simplifies the real problem somewhat. In the final implementation, the user of my library has to implement getWater() and therefore can make mistakes. So, since I got to assume that this will happen, I needed to assure that no nil values are in the slice. I anticipated that users can return pointer to structs or structs that implemented the interface.... Commented Mar 19, 2015 at 7:42
  • 10
    A nil pointer is not an invalid value. Note that you can call methods on nil pointers just fine: play.golang.org/p/Agd8eIwaKZ Try reading my post about nil pointer and nil interfaces, it may help: npf.io/2014/05/intro-to-go-interfaces/#toc_4 Commented Mar 19, 2015 at 17:59