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

如何在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]++
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?