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

报告相同问题?

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到