FAQ
Hi all,

When slice is nil, appending its value is OK:

var s []int
s = append(s, 1)

But when a map is nil, assigning value is not OK, and cause panic:

var m map[string]int
m["Alex"] = 16

Why does golang adopt this design?

Best Regards
Nan Xiao

--
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+unsubscribe@googlegroups.com.

## Search Discussions

•  at Mar 29, 2016 at 10:33 am ⇧
It's kind of a complex question, but the shortest answer is append retuns a new sloce value, where map operations mutate and existing map value.

In the former case, you're getting a new slice value, so it's reaosnable to take a nil slice and return a non nil slice.

If mapa worked that way, map assignment would have to work like append

m = m["key"] = "value"

Which would be quite odd, to say the least.

--
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+unsubscribe@googlegroups.com.
•  at Mar 29, 2016 at 10:47 am ⇧

On Tue, Mar 29, 2016 at 8:11 PM, Nan Xiao wrote:
Hi all,

When slice is nil, appending its value is OK:

var s []int
s = append(s, 1)

But when a map is nil, assigning value is not OK, and cause panic:

var m map[string]int
m["Alex"] = 16

Why does golang adopt this design?
map values act like pointers, so all copies of a map value point to
the same map. For keys on a nil map to be assignable you would need an
additional level of indirection, so that all the other copies of the
map value can point to the newly allocated map.

This adds an extra cost to every map lookup.

--
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+unsubscribe@googlegroups.com.
•  at Mar 29, 2016 at 1:19 pm ⇧
nil map doesn't point to an initialized map. Assigning value won't

The append function appends the elements x to the end of the slice s, and *grows
the slice if a greater capacity is needed*. It reallocates the resulting
slice describes a completely different array.

在 2016年3月29日星期二 UTC+8下午5:11:59，Nan Xiao写道：
Hi all,

When slice is nil, appending its value is OK:

var s []int
s = append(s, 1)

But when a map is nil, assigning value is not OK, and cause panic:

var m map[string]int
m["Alex"] = 16

Why does golang adopt this design?

Best Regards
Nan Xiao
--
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+unsubscribe@googlegroups.com.

## Related Discussions

Discussion Overview
 group golang-nuts categories go posted Mar 29, '16 at 9:12a active Mar 29, '16 at 1:19p posts 4 users 4 website golang.org

### 4 users in discussion

Content

People

Support

Translate

site design / logo © 2021 Grokbase