What is the equivalent of a Java ArrayList<E> in Golang?

arraylist, go, java

Solution

Use a slice:

var channels []Channel  // an empty list
channels = append(channels, Channel{name:"some channel name"})

Also, your Channel declaration is slightly off, you need the 'type' keyword:

type Channel struct {
    name string
}

Here's a complete example: http://play.golang.org/p/HnQ30wOftb

For more info, see the slices article.

There's also the go tour (tour.golang.org) and the language spec (golang.org/ref/spec, see #Slice_types, #Slices, and #Appending_and_copying_slices).

Problem

In my particular use case, I would like to know how the following Java code would be implemented in Go - ``` class Channel { public String name; public Channel(){} } ArrayList<Channel> channels = new ArrayList<Channel>(); ``` I've gotten started, and I think this would be the appropriate struct for Channel in Go - ``` struct Channel { name string } ``` I just need to know how ArrayList would work in Go

Original source