96

I come from a C++ background and I'm used to using the std::vector class for things like this. Lets assume I want a dynamic array of these:

type a struct {
    b int
    c string
}

What is the standard way of doing this?

A snippet would be very useful

1

7 Answers 7

105

Use the append() builtin

Example:

type mytype struct {
  a, b int
}

func main() {
  a := []mytype{mytype{1, 2}, mytype{3, 4}}
  a = append(a, mytype{5, 6})
}

Refer to the spec for more info on append.

4
  • One more thing cthom06, if you would: I am using "for _, t = range(msgs)" where msgs is a vector. I then have to cast t to my msg struct. Can i Do this all inside the for statement on 1 line?
    – user181351
    Commented Aug 2, 2010 at 12:26
  • @tm1rbrt I don't believe so. At least not with range.
    – cthom06
    Commented Aug 2, 2010 at 12:30
  • 3
    Note that the container/vector package is now gone. It has been supplanted by vanilla slices and the append function. Slices essentially are resizable arrays. Commented Mar 19, 2012 at 3:35
  • @JeremyWall indeed, fixed it since it is the accepted answer.
    – cthom06
    Commented Mar 27, 2012 at 20:59
76

A Go Slice contains three elements: data, length, and capacity.

s := make([]int, 0, 10)

The variable s is a slice of ints with a length of 0 and a capacity of 10. The built-in len() and cap() functions allow you to get the length and capacity of a slice:

len(s) == 0
cap(s) == 10

To increase the length of a slice, simply re-slice:

s = s[0:5]
// len(s) == 5
// cap(s) == 10

To decrease the length, you can take a sub-slice:

s = s[0:1]
// len(s) == 1

There are some shorter ways to invoke make():

a := make([]int, 10) 
// len(a) == cap(a) == 10

b := make([]int)
// len(b) == cap(b) == 0

That's all well and good, but what if you need to increase the length of a slice beyond its capacity? To do that, you need to allocate a new slice and copy the contents of the old slice to the new one. (The function "copy" is another built-in.)

t := make([]int, len(s), 20)
copy(t, s)

The Effective Go document takes this example a bit further, implementing an Append function that appends one slice to another, resizing it if necessary.

Slices are backed by arrays; when you make() a slice of a specific capacity, an array of that capacity is allocated in the background. The slice effectively becomes a "smart pointer" to that array. If you pass that slice (or a subslice of that slice) to another function, it is passed as a pointer to that same array. This makes sub-slices very cheap to create - it's the allocation of the backing array that is expensive.

The Go standard library includes a number of container packages - vector, for instance - that eliminate the need to manually manage slices. Use slices for speed, and more elaborate container classes for convenience. (Saying that, I still use slices for most things.)

You may be wondering why you need to go to all this trouble. After all, a lot of languages provide dynamically resized arrays as primitives. The reason for this is tied to Go's philosophy. The language designers don't presume to know what the appropriate allocation policy is for your program; instead they give you the tools you need to build your own data structures.

3
  • If you recreate the slice every time the slice grows, it will eat up a lot of time. However, if you double the slice capacity every time it grows beyond its capacity, it will probably not require as much time to insert new elements into the slice. t := make([]int, 5, 10) u := make([]int, 10, 20) etc...
    – Xeoncross
    Commented May 24, 2014 at 21:54
  • 1
    @Xeoncros, the append builtin already is reasonably smart about allocating extra space; just use it.
    – Dave C
    Commented Jul 20, 2015 at 20:05
  • 3
    be aware that this part is obsolete: "increase the length of a slice beyond its capacity" ... "you need to allocate a new slice and copy the contents of the old slice to the new one". now the append() does the reallocation. see answers by cthom06 and Jessta.
    – minghua
    Commented Sep 23, 2016 at 19:30
32

The idiomatic way to do this has changed. The addition of the built-in append() function means that you can extend a slice like so:

type a struct {
    b int
    c string
}

func main(){
    var mySlice []a
    mySlice = append(mySlice,a{5,"pizza"})
}

Append() will append the given item to the slice if there is room or extend the slice if it's not bigger enough.

More information about append() is here http://golang.org/doc/go_spec.html#Appending_and_copying_slices

6

For a Simpler Example of the append() builtin

friends := []string{"Adam"}

friends = append(friends, "Rahul") // Add one friend or one string
        
friends = append(friends, "Angelica", "Rashi") // Add multiple friends or multiple strings

append() Documentation here

0
2

you might also be able to make do with a slice. which is an array that knows its current length. And can have a separate current length and maximum capacity. Note the values passed for initial size and capacity do not have to be constants so you can create a function which builds and returns slices of different lengths based on its parameters.

The up side is that a slice []Int can just be indexed like an array, and will return ints when used in this way.

The downside is that it will not automatically grow byound its stated capacity. Effective Go has an example of how you would go about handling reallocation.

the code would be

type mytype struct {
   a, b int
}




func main() {

  sl := make([]mytype, 10, 50) //slice of 10 items, max capacity 50 these do not have to be constant expressions.
  sl[0] = mytype{1,2}
   //...
  for i, value := range sl {
  // ... do stuff with value
  }
}
0

Hi we can simply do this in two ways

type mytype struct {
  a, b int
}

Just do like this

  1. Without append

__

a := []mytype{mytype{1, 2}, mytype{3, 4}, mytype{4, 5}}
  1. With append

__

a:=  append([]mytype{}, mytype{1, 2}, mytype{3, 4}, mytype{4, 5})

Add as much as you want. First one is an easy way to do this. Hope this will help you.

1
  • I'm not sure why this voted down. It is a twist on the accepted answer.
    – user12817546
    Commented Oct 10, 2020 at 6:01
0

If you want to have a dynamic List in Go then you have to use Slice

you can learn more about it here: https://blog.golang.org/slices-intro

Some Examples: https://tour.golang.org/moretypes/7 https://gobyexample.com/slices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.