我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

如何在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
    }
}

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]++
}

近期文章

  • GAE go用户注销异常行为
  • 如果Go sql.DB关闭,是否关闭了所有未关闭的准备查询?
  • 指向空接口类型断言的指针
  • 在Go中序列化混合类型JSON数组
  • golang配置文件runtime.usleep占70%
  • 使用Golang进行HTML验证
  • 如何为CI组织两个git repos
  • 转到:如何读取两个文件的内容并连接为字符串
  • JSP中写的JavaScript 函数调用了起不了作用,是怎么回事?
  • 在Go中的EOF上重新连接TCP
  • 在TravisCI中的Go中创建带有扩展名的临时文件
  • 如何将结构作为参数传递给xml-rpc
  • 直接从一个频道发送到另一个频道
  • 从Go中的可变参数创建带有可选字段的类型
  • 带有OAuth2和Google / Facebook身份验证的Golang API
  • 将RFC3339时间存储在MySQL数据库中时,最佳实践是什么?
  • 将JSON成员字符串转换为JSON对象
  • 不使用Sprintf的Stringer实现
  • :=运算符和Golang中的if语句
  • 无法使用非字符串键对json.marshal映射

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华