FAQ
From golang book (http://www.golang-book.com/6/index.htm):
"A slice cannot be grown beyond its capacity. Attempting to do so will
cause a runtime panic, just as when indexing outside the bounds of a slice
or array. Similarly, slices cannot be re-sliced below zero to access
earlier elements in the array"
Than, can someone explain how's the following work?
//make array af len=0, capacity=0
slice := make([]string, 0)
....
slice = append(slice, "one")

--
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

  • Bruno Albuquerque at Apr 1, 2015 at 5:53 pm
    Append creates a new slice when it needs to grown it being capacity and
    copies the elements of the original slice to the new one.

    Em qua, 1 de abr de 2015 às 14:49, <vlyamtsev@gmail.com> escreveu:

    From golang book (http://www.golang-book.com/6/index.htm):
    "A slice cannot be grown beyond its capacity. Attempting to do so will
    cause a runtime panic, just as when indexing outside the bounds of a slice
    or array. Similarly, slices cannot be re-sliced below zero to access
    earlier elements in the array"
    Than, can someone explain how's the following work?
    //make array af len=0, capacity=0
    slice := make([]string, 0)
    ....
    slice = append(slice, "one")

    --
    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.
    --
    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.
  • Bruno Albuquerque at Apr 1, 2015 at 5:54 pm
    And, BTW, thats why you have to do it like you did:

    slice = append(...)

    The returned slice might be a new slice that is different from the original
    you had.



    Em qua, 1 de abr de 2015 às 14:53, Bruno Albuquerque <bga@bug-br.org.br>
    escreveu:
    Append creates a new slice when it needs to grown it being capacity and
    copies the elements of the original slice to the new one.

    Em qua, 1 de abr de 2015 às 14:49, <vlyamtsev@gmail.com> escreveu:

    From golang book (http://www.golang-book.com/6/index.htm):
    "A slice cannot be grown beyond its capacity. Attempting to do so will
    cause a runtime panic, just as when indexing outside the bounds of a slice
    or array. Similarly, slices cannot be re-sliced below zero to access
    earlier elements in the array"
    Than, can someone explain how's the following work?
    //make array af len=0, capacity=0
    slice := make([]string, 0)
    ....
    slice = append(slice, "one")

    --
    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.
    --
    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.
  • Caleb Spare at Apr 1, 2015 at 5:54 pm
    That quote is from the golang.org blog post "Go Slices: usage and
    internals", not the book you linked:

    http://blog.golang.org/go-slices-usage-and-internals

    The reason it works is that you made a new slice value and assigned it to
    the variable named "slice".
    On Wed, Apr 1, 2015 at 10:49 AM, wrote:

    From golang book (http://www.golang-book.com/6/index.htm):
    "A slice cannot be grown beyond its capacity. Attempting to do so will
    cause a runtime panic, just as when indexing outside the bounds of a slice
    or array. Similarly, slices cannot be re-sliced below zero to access
    earlier elements in the array"
    Than, can someone explain how's the following work?
    //make array af len=0, capacity=0
    slice := make([]string, 0)
    ....
    slice = append(slice, "one")

    --
    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.
    --
    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
postedApr 1, '15 at 5:49p
activeApr 1, '15 at 5:54p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase