How to increase the length of a slice

11,286 views
Skip to first unread message

venkatesh reddy

unread,
Oct 8, 2013, 8:35:51 PM10/8/13
to golan...@googlegroups.com
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..

Dave Cheney

unread,
Oct 8, 2013, 8:36:46 PM10/8/13
to venkatesh reddy, golang-nuts
Can you please explain why you do not want to use append ? That is its function.

Kevin Gillette

unread,
Oct 8, 2013, 9:26:14 PM10/8/13
to golan...@googlegroups.com
I think you're misinterpreting what append does. If there's available cap, then append does "create a new slice".

venkatesh reddy

unread,
Oct 8, 2013, 9:40:00 PM10/8/13
to golan...@googlegroups.com, venkatesh reddy
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?

Dave Cheney

unread,
Oct 8, 2013, 9:42:18 PM10/8/13
to venkatesh reddy, golang-nuts
> 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> 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.
> For more options, visit https://groups.google.com/groups/opt_out.

venkatesh reddy

unread,
Oct 8, 2013, 9:43:44 PM10/8/13
to golan...@googlegroups.com

Rodrigo Kochenburger

unread,
Oct 8, 2013, 9:47:13 PM10/8/13
to Dave Cheney, venkatesh reddy, golang-nuts
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.


--
- RK

venkatesh reddy

unread,
Oct 8, 2013, 9:51:18 PM10/8/13
to golan...@googlegroups.com, venkatesh reddy
thanks ..:)
I l go through the link..

Ugorji Nwoke

unread,
Oct 8, 2013, 9:54:01 PM10/8/13
to golan...@googlegroups.com
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.

Rob Pike

unread,
Oct 9, 2013, 12:01:57 AM10/9/13
to Ugorji Nwoke, golan...@googlegroups.com
I wrote blog.golang.org/slices to answer this very question. Please read it.

-rob

you fu

unread,
Oct 9, 2013, 9:47:45 AM10/9/13
to golan...@googlegroups.com, Ugorji Nwoke
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?

Ugorji Nwoke

unread,
Oct 9, 2013, 9:52:21 AM10/9/13
to golan...@googlegroups.com, Ugorji Nwoke


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. 
Reply all
Reply to author
Forward
0 new messages