doulu4413 2012-04-08 05:55
浏览 260
已采纳

Golang中的Java ArrayList <E>等效于什么?

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

  • 写回答

3条回答 默认 最新

  • doutu1889 2012-04-08 06:29
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?