FAQ
is there any way to increase the length of a slice in go language
I dont want to append the slice to a new slice, I want to increase the
length of an existing slice , with out using a new slice
is that possible ...please help me..

--
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/groups/opt_out.

Search Discussions

  • Dave Cheney at Oct 9, 2013 at 12:36 am

    On Wed, Oct 9, 2013 at 11:35 AM, venkatesh reddy wrote:
    is there any way to increase the length of a slice in go language
    I dont want to append the slice to a new slice, I want to increase the
    length of an existing slice , with out using a new slice
    is that possible ...please help me..
    Can you please explain why you do not want to use append ? That is its function.

    --
    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/groups/opt_out.
  • Venkatesh reddy at Oct 9, 2013 at 1:40 am
    I just want to increase the legth of slice and allocate new elements to
    it..
    but I did not find any way to do it ..
    so I just found this code ..
    slice1 := []int{1,2,3}
    slice2 := append(slice1, 4, 5)

    which copies copies slice1 elements and new elements 4,5 to a new slice..


    but I dont want to create a new slice..just want to increase the length of
    existing slice..
    is it possible?

    On Tuesday, 8 October 2013 18:36:46 UTC-6, Dave Cheney wrote:

    On Wed, Oct 9, 2013 at 11:35 AM, venkatesh reddy
    <venkateshre...@gmail.com <javascript:>> wrote:
    is there any way to increase the length of a slice in go language
    I dont want to append the slice to a new slice, I want to increase the
    length of an existing slice , with out using a new slice
    is that possible ...please help me..
    Can you please explain why you do not want to use append ? That is its
    function.
    --
    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/groups/opt_out.
  • Dave Cheney at Oct 9, 2013 at 1:42 am

    slice1 := []int{1,2,3}
    creates a slice of ints with a length of 3 and capacity of 3
    slice2 := append(slice1, 4, 5)
    must create a new slice, copy the existing 3 elements to this new
    slice, then append 4 and 5, leaving you with a length of 5 and a
    capacity of at least 5.
    which copies copies slice1 elements and new elements 4,5 to a new slice..


    but I dont want to create a new slice..just want to increase the length of
    existing slice..
    is it possible?
    It is not possible to do what you want. Have you read
    http://blog.golang.org/slices ?
    On Tuesday, 8 October 2013 18:36:46 UTC-6, Dave Cheney wrote:

    On Wed, Oct 9, 2013 at 11:35 AM, venkatesh reddy
    wrote:
    is there any way to increase the length of a slice in go language
    I dont want to append the slice to a new slice, I want to increase the
    length of an existing slice , with out using a new slice
    is that possible ...please help me..
    Can you please explain why you do not want to use append ? That is its
    function.
    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Rodrigo Kochenburger at Oct 9, 2013 at 1:47 am
    Keep in mind that any other language that have dynamic arrays will do
    something similar behind the scenes. The difference is that go gives you
    the control of how and when to grow it.

    Arrays (and slices) are contiguous area of memory, which means that you can
    not simply grow it if you don't have free space in the end of it, which in
    a go slice would be the capacity.
    On Tuesday, October 8, 2013, Dave Cheney wrote:

    slice1 := []int{1,2,3}
    creates a slice of ints with a length of 3 and capacity of 3
    slice2 := append(slice1, 4, 5)
    must create a new slice, copy the existing 3 elements to this new
    slice, then append 4 and 5, leaving you with a length of 5 and a
    capacity of at least 5.
    which copies copies slice1 elements and new elements 4,5 to a new slice..

    but I dont want to create a new slice..just want to increase the length of
    existing slice..
    is it possible?
    It is not possible to do what you want. Have you read
    http://blog.golang.org/slices ?
    On Tuesday, 8 October 2013 18:36:46 UTC-6, Dave Cheney wrote:

    On Wed, Oct 9, 2013 at 11:35 AM, venkatesh reddy
    <venkateshre...@gmail.com <javascript:;>> wrote:
    is there any way to increase the length of a slice in go language
    I dont want to append the slice to a new slice, I want to increase the
    length of an existing slice , with out using a new slice
    is that possible ...please help me..
    Can you please explain why you do not want to use append ? That is its
    function.
    --
    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 <javascript:;>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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 <javascript:;>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    - RK

    --
    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/groups/opt_out.
  • Venkatesh reddy at Oct 9, 2013 at 1:51 am
    thanks ..:)
    I l go through the link..

    On Tuesday, 8 October 2013 19:42:18 UTC-6, Dave Cheney wrote:

    slice1 := []int{1,2,3}
    creates a slice of ints with a length of 3 and capacity of 3
    slice2 := append(slice1, 4, 5)
    must create a new slice, copy the existing 3 elements to this new
    slice, then append 4 and 5, leaving you with a length of 5 and a
    capacity of at least 5.
    which copies copies slice1 elements and new elements 4,5 to a new slice..

    but I dont want to create a new slice..just want to increase the length of
    existing slice..
    is it possible?
    It is not possible to do what you want. Have you read
    http://blog.golang.org/slices ?
    On Tuesday, 8 October 2013 18:36:46 UTC-6, Dave Cheney wrote:

    On Wed, Oct 9, 2013 at 11:35 AM, venkatesh reddy
    wrote:
    is there any way to increase the length of a slice in go language
    I dont want to append the slice to a new slice, I want to increase
    the
    length of an existing slice , with out using a new slice
    is that possible ...please help me..
    Can you please explain why you do not want to use append ? That is its
    function.
    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.
  • Kevin Gillette at Oct 9, 2013 at 1:26 am
    I think you're misinterpreting what append does. If there's available cap,
    then append does "create a new slice".
    On Tuesday, October 8, 2013 6:35:51 PM UTC-6, venkatesh reddy wrote:

    is there any way to increase the length of a slice in go language
    I dont want to append the slice to a new slice, I want to increase the
    length of an existing slice , with out using a new slice
    is that possible ...please help me..
    --
    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/groups/opt_out.
  • Venkatesh reddy at Oct 9, 2013 at 1:43 am

    On Tuesday, 8 October 2013 19:26:14 UTC-6, Kevin Gillette wrote:
    I think you're misinterpreting what append does. If there's available cap,
    then append does "create a new slice".
    On Tuesday, October 8, 2013 6:35:51 PM UTC-6, venkatesh reddy wrote:

    is there any way to increase the length of a slice in go language
    I dont want to append the slice to a new slice, I want to increase the
    length of an existing slice , with out using a new slice
    is that possible ...please help me..
    --
    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/groups/opt_out.
  • Ugorji Nwoke at Oct 9, 2013 at 1:54 am
    Short answer is that you cannot modify the length or capacity of a slice
    value object.

    Let me illustrate how I think about this.

    A slice is a 3-word value, with the 3 words being length, capacity (how far
    length can go before being out of bounds of the backing array), and a
    pointer to the first element of the slice in the backing array.

    In typical usage, you don't modify length and capacity of a slice. These
    are only changed by re-slicing and append, both of which return a new
    3-word slice value.

    The append call may allocate a new backing array, if the number of items to
    append moves the length past its capacity.

    e.g.
    var a [8]int
    var b = a[2:6] // now b's backing array is a, with len=4, cap=6, and ptr to
    first element is a[2]
    var c = append(b, 7) // now c's backing array is a, with len=5, cap=6, and
    ptr to first element is a[2]
    var d = append(c, 8, 9, 10) // now d's backing array is a new allocated
    array, with len=8, cap=UNKNOWN (but retrievalable via cap function), and
    ptr to first element is UNKNOWN

    The only way I know to possibly modify length without creating a new slice
    value, is the reflect.SetLen and reflect.SetCap calls. However, reflection
    does its own allocation which means you save nothing really.
    On Tuesday, October 8, 2013 8:35:51 PM UTC-4, venkatesh reddy wrote:

    is there any way to increase the length of a slice in go language
    I dont want to append the slice to a new slice, I want to increase the
    length of an existing slice , with out using a new slice
    is that possible ...please help me..
    --
    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/groups/opt_out.
  • Rob Pike at Oct 9, 2013 at 4:02 am
    I wrote blog.golang.org/slices to answer this very question. Please read it.

    -rob

    --
    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/groups/opt_out.
  • You fu at Oct 9, 2013 at 1:47 pm
    the code:

    func Extend(slice []int, element int) []int { n := len(slice) if n == cap(slice) { // Slice is full; must grow. // We double its size and add 1, so if the size is zero we still grow. newSlice := make([]int, len(slice), 2*len(slice)+1) copy(newSlice, slice) slice = newSlice } slice = slice[0 : n+1] slice[n] = element return slice}

    the statement slice = slice[0 : n+1],

    if the n < cap(slice),the slice[0:n+1] only increate the slice lenght.right?

    On Wednesday, October 9, 2013 12:01:57 PM UTC+8, Rob Pike wrote:

    I wrote blog.golang.org/slices to answer this very question. Please read
    it.

    -rob
    --
    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/groups/opt_out.
  • Ugorji Nwoke at Oct 9, 2013 at 1:52 pm

    On Wednesday, October 9, 2013 9:47:45 AM UTC-4, you fu wrote:
    the code:

    func Extend(slice []int, element int) []int { n := len(slice) if n == cap(slice) { // Slice is full; must grow. // We double its size and add 1, so if the size is zero we still grow. newSlice := make([]int, len(slice), 2*len(slice)+1) copy(newSlice, slice) slice = newSlice } slice = slice[0 : n+1] slice[n] = element return slice}

    the statement slice = slice[0 : n+1],

    if the n < cap(slice),the slice[0:n+1] only increate the slice lenght.right?


    Yea, but you created a new 3-word slice value and assigned it to the same
    variable.

    If you had written:
    slice2 := slice[0:n+1], it would be clearer that the old slice variable is
    not changed.

    This is why you always return the slice, and why append function returns a
    new slice each time.

    On Wednesday, October 9, 2013 12:01:57 PM UTC+8, Rob Pike wrote:

    I wrote blog.golang.org/slices to answer this very question. Please read
    it.

    -rob
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 9, '13 at 12:35a
activeOct 9, '13 at 1:52p
posts12
users7
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase