在我的特定用例中,我想知道如何在Go中实现以下Java代码:
class Channel {
public String name;
public Channel(){}
}
ArrayList<Channel> channels = new ArrayList<Channel>();我已经开始了,我认为这将是Go中Channel的合适结构-
struct Channel {
name string
}我只想知道ArrayList在Go中是如何工作的
发布于 2012-04-08 14:29:04
使用切片:
var channels []Channel // an empty list
channels = append(channels, Channel{name:"some channel name"})另外,您的Channel声明稍有偏差,您需要'type‘关键字:
type Channel struct {
name string
}下面是一个完整的示例:http://play.golang.org/p/HnQ30wOftb
有关更多信息,请参阅slices article。
还有go教程(tour.golang.org)和语言规范(golang.org/ref/ spec,请参阅#Slice_types、#Slices和#Appending_and_copying_slices)。
发布于 2012-04-10 09:16:04
使用切片。
有关常见切片习惯用法的详细信息,请参阅the "Slice Tricks" wiki page。
发布于 2017-05-13 11:12:52
This works
//make the object Channel
type Channel struct {
name string
}
// a empty list
var channels = []*Channel {}
//and finally add this object
channels = append(channels, Channel{name:"juan carlos anez mejias"})https://stackoverflow.com/questions/10060728
复制相似问题