Go 语言中 range 关键字用于 for 循环中迭代数组(array)、切片(slice)、通道(channel)或集合(map)的元素。在数组和切片中它返回元素的索引和索引对应的值,在集合中返回 key-value 对的 key 值。

实例

实例

package main
import "fmt"
func main() {
    //这是我们使用range去求一个slice的和。使用数组跟这个很类似
    nums := []int{2, 3, 4}
    sum := 0
    for _, num := range nums {
        sum += num
    }
    fmt.Println("sum:", sum)
    //在数组上使用range将传入index和值两个变量。上面那个例子我们不需要使用该元素的序号,所以我们使用空白符"_"省略了。有时侯我们确实需要知道它的索引。
    for i, num := range nums {
        if num == 3 {
            fmt.Println("index:", i)
        }
    }
    //range也可以用在map的键值对上。
    kvs := map[string]string{"a": "apple", "b": "banana"}
    for k, v := range kvs {
        fmt.Printf("%s -> %s\n", k, v)
    }
    //range也可以用来枚举Unicode字符串。第一个参数是字符的索引,第二个是字符(Unicode的值)本身。
    for i, c := range "go" {
        fmt.Println(i, c)
    }
}

GO Range的更多相关文章

  1. SQL Server 合并复制遇到identity range check报错的解决

        最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...

  2. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  3. [LeetCode] Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

  4. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  5. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  6. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  7. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  8. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  10. C++11中自定义range

    python中的range功能非常好用 for i in range(100): print(i) 现在利用C++11的基于范围的for循环特性实现C++中的range功能 class range { ...

随机推荐

  1. php设计模式之面向接口开发实例代码

    <?php header("Content-type:text/html;charset=utf-8"); /** * 共同接口 */ interface db { func ...

  2. eclipse非正常关闭,再打开后报错

    Previous operation has not finished; run 'cleanup' if it was interrupted 启动任务管理器,将javaw.exe进程杀死,然后重启 ...

  3. 等差数列Arithmetic Progressions题解(USACO1.4)

    Arithmetic Progressions USACO1.4 An arithmetic progression is a sequence of the form a, a+b, a+2b, . ...

  4. Linux - 文件时间戳

    概述 简介 linux 文件时间戳 背景 最近感觉很消极的样子 心情不好加不知道写啥 随便水一水 能水的就那么多, 水一次, 少一次 环境 os centos7 1. 时间戳 概述 简述 时间戳 li ...

  5. EAC3 enhanced channel coupling

    Enhanced channel coupling是一种spatial coding 技术,在传统的channel coupling的基础上添加了phase compensation, de-corr ...

  6. JSP读取数据库二进制图片并显示

    用JSP从数据库中读取二进制图片并显示在网页上 环境mysql+tomcat: 先在mysql下建立如下的表. 并存储了二进制图像(二进制格式存储图片可以参考我的另一篇博客:https://www.c ...

  7. RegExp-named captured groups(命名分组捕获)

    console.log('2020-01-23'.match(/(\d{4})-(\d{2})-(\d{2})/)) const t = '2020-01-23'.match(/(?<year& ...

  8. php提供下载服务实例

    两个步骤:1,通过header头信息告诉浏览器,我给你回应的是一个附件请接收 2,通过php读取下载的文件的内容并返回 前端 <!DOCTYPE html> <html lang=& ...

  9. Win2012或Win2016安装网卡

    在电脑安装了Windows Server 2012或者2016等系统之后,可能面临的很大的问题就是没有有线或无线网卡. 1.安装网卡的功能 2.一直到“功能”这部分,选择安装“DirectPlay”和 ...

  10. FromBase64String 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符

    js前台: <input id="upload_img_input" v-on:change="onFileChange" type="file ...