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

后端 未结 7 1608
猫巷女王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 15:54

    How about use defer:

    s := []int{5, 4, 3, 2, 1}
    for i, _ := range s {
       defer fmt.Println(s[i])
    }
    

提交回复
热议问题