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.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney 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.
    For more options, visit https://groups.google.com/d/optout.
  • Jesse McNelis 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.
    For more options, visit https://groups.google.com/d/optout.
  • Chuck bartowski at Mar 29, 2016 at 1:19 pm
    nil map doesn't point to an initialized map. Assigning value won't
    reallocate point address.

    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.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 29, '16 at 9:12a
activeMar 29, '16 at 1:19p
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase