注册
登录
go
Go中有一个foreach循环吗?
返回
Go中有一个foreach循环吗?
作者:
狗头军师
发布时间:
2024-12-23 09:53:18 (1月前)
foreachGo语言中是否有结构?我可以使用for?遍历切片或数组吗?
收藏
举报
2 条回复
1#
回复此人
v-star*위위
|
2020-07-27 16-59
https://golang.org/ref/spec#For_range 带有“ range”子句的“ for”语句遍历数组,切片,字符串或映射的所有条目,或通道上接收到的值。对于每个条目,它将迭代值分配给相应的迭代变量,然后执行该块。 举个例子: ``` for index, element := range someSlice { // index is the index where we are // element is the element from someSlice for where we are } ``` 如果您不关心索引,则可以使用_: ``` for _, element := range someSlice { // element is the element from someSlice for where we are } ``` 下划线_是空白标识符,一个匿名占位符。
编辑
登录
后才能参与评论