有没有办法在Go中反向切片? [英] Is there a way to iterate over a slice in reverse in Go?

查看:124
本文介绍了有没有办法在Go中反向切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for _,反向范围内的元素mySlice {
...
}


解决方案

否没有方便的操作员为此添加一个到位的范围。你必须做一个正常的循环倒计数:

  s:= [] int {5,4,3, 2,1} 
for i:= len(s)-1; i> = 0; i-- {
fmt.Println(s [i])
}


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

for _, element in reverse range mySlice {
        ...
}

解决方案

No there is no convenient operator for this to add to the range one in place. You'll have to do a normal for loop counting down:

s := []int{5, 4, 3, 2, 1}
for i := len(s)-1; i >= 0; i-- {
   fmt.Println(s[i])
}

这篇关于有没有办法在Go中反向切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆