Is there a way to iterate over a slice in reverse in Go?

后端 未结 7 1611
猫巷女王i
猫巷女王i 2021-01-30 15:28

It would be convenient to be able to say something like:

for _, element := reverse range mySlice {
        ...
}
7条回答
  •  别那么骄傲
    2021-01-30 16:06

    Variation with index

    for k := range s {
            k = len(s) - 1 - k
            // now k starts from the end
        }
    

提交回复
热议问题