duannan3959
2018-11-27 20:01
浏览 308
已采纳

如何在Golang中增加数组或切片中的所有元素

Below code increments only ith element of that slice. Is there something inbuild that i can increment all elements with 1. Please suggest.

for i:= 0; i< k ;i++{
    if(slice[i] < K){
        slice[i] = slice[i] + 1
    }
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtao9095 2018-11-27 20:31
    已采纳

    While working with slices you will find that you will gravitate towards for loops. Go doesn't have extra functions for slices that you might find with other languages.

    for i := range slice {
      slice[i]++
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题