Nil slices are an example of an ideal zero value. When you use them, you 
don't need to check for nil -> its zero value is already usable.

Exactly the "slices" case (and the "string" type) is the one inspiring me 
to search for more useful zero values for certain types. The two I am 
thinking more of lately are:

   - *Pointers*, the reason for this thread
   - *Maps*.  I would *love* maps to have a useful zero value similar to 
   slices. This way, and among other reasons, I would not need to write many 
   constructor-like functions ("func NewX") I write now because my structs 
   have a field that is a map (and hence, needs to be initialized to avoid nil 
   pointer errors).



El sábado, 15 de febrero de 2020, 19:03:55 (UTC), Jake Montgomery escribió:
>
> I see it is a nice property, but I'd say only for people writing the 
>> compiler. I adventure [sic] to say that people using the language won't 
>> care too much about this property. Having a useful zero-value (in this 
>> case, an initialized pointer),  like we have with strings for example, 
>> would be a very nice property for language users
>>
>
> I think that Tristan was pointing out that nil *can *be a useful zero 
> value, since you can still call methods on a nil struct, if the method was 
> written to handle it. Likewise nil slices are pretty much as useful as 
> empty slices, etc.   
>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b73e57b8-2f27-48f8-8d68-4effa5d4f08a%40googlegroups.com.

Reply via email to