foreachGo语言中有结构吗?我可以用 a 迭代切片或数组for吗? % D; a, Z2 i* e& k- q4 z r$ x7 X2 X0 p : E3 Y9 a, O6 M( O2 t 解决方案: & e, B6 F/ l( p, B; G
带有“range”子句的“for句子遍历了数组、切片、字符串或映射的所有项目,或通道上接收到的值。对于每个项目,它将迭代值分配给相应的迭代变量,然后执行该块。( r. Y+ c, d& z7 \) c! R
举个例子: 8 N) t; Y' u0 u% {- m
for index,element := range someSlice / index is the index where we are // element is the element from someSlice for where we are}% c, r S7 M* K3 {3 z! b
如果不关心索引,可以用_:# d* q3 t0 K* C$ t. \
for _,element := range someSlice / element is the element from someSlice for where we are} 1 }. c# X+ b. f( [1 q- b; |