lia*_*dee 15 java arraylist go
在我的特定用例中,我想知道如何在Go中实现以下Java代码 -
class Channel {
public String name;
public Channel(){}
}
ArrayList<Channel> channels = new ArrayList<Channel>();
Run Code Online (Sandbox Code Playgroud)
我已经开始了,我认为这将是Go in Channel的合适结构 -
struct Channel {
name string
}
Run Code Online (Sandbox Code Playgroud)
我只需要知道ArrayList如何在Go中工作
小智 35
使用切片:
var channels []Channel // an empty list
channels = append(channels, Channel{name:"some channel name"})
Run Code Online (Sandbox Code Playgroud)
此外,您的频道声明略有偏差,您需要'type'关键字:
type Channel struct {
name string
}
Run Code Online (Sandbox Code Playgroud)
这是一个完整的例子:http://play.golang.org/p/HnQ30wOftb
有关详细信息,请参阅切片文章.
还有go tour(tour.golang.org)和语言规范(golang.org/ref/spec,请参阅#Slice_types,#Slices和#Appending_and_copying_slices).
| 归档时间: |
|
| 查看次数: |
18092 次 |
| 最近记录: |