A "usable" nil map, i.e. being able to insert into it straight away, I 
sympathise with.  Remember though that you can't append "in place" to a nil 
slice: it returns a new slice object.  https://play.golang.org/p/dL-r74C5m_w

I presume you don't want to write the same for maps as you do for slices:

m2 := m.set(42, "hello")   // instead of: m[42] = "hello"

I suppose in principle it might be possible to pre-initialize all variables 
and structs which contain a map to a dynamically-allocated instance of that 
map type.

However, I don't think the idea extends to pointers:

1. you are asking for all pointers to be pre-initialized to some 
dynamically-generated object, which might need to be garbage-collected 
immediately the pointer is changed to point to something else.  That would 
be very wasteful.

2. there's the problem with self-referential pointers, e.g. A contains *B 
and B contains *A.

3. you lose the "nil-ness" property of pointers: the ability of pointers to 
point to nothing at all (such as the tree example I gave).  If you could no 
longer point to nothing, how could you tell the difference between an 
interior node and a leaf node of a tree?  You would have to change all code 
to use, say, a slice of pointers (of size 0 or 1) instead of a pointer.

Finally, what would you do about nil interface variables? You don't even 
know what *type* the value should have.

>

-- 
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/60d7d0e7-bc28-485d-b238-7460329c6d28%40googlegroups.com.

Reply via email to