Golang学习笔记_15——range

Golang学习笔记_12------结构体
Golang学习笔记_13------数组
Golang学习笔记_14------切片


文章目录

    • range
      • [1. 介绍](#1. 介绍)
      • [2. 适用场景](#2. 适用场景)
      • [3. 注意事项](#3. 注意事项)
      • 4.示例
        • [4.1 遍历数组和切片](#4.1 遍历数组和切片)
        • [4.2 遍历字符串](#4.2 遍历字符串)
        • [4.3 遍历Map](#4.3 遍历Map)
        • [4.4 遍历通道](#4.4 遍历通道)
    • 源码

range

1. 介绍

在Go语言中,range 关键字用于遍历数组、切片、字符串、映射(map)、通道(channel)等数据结构。

它提供了一种简洁且强大的方式来迭代这些集合中的元素。

2. 适用场景

  1. 遍历数组和切片:
    • 当你需要访问集合中每个元素及其索引时。
    • 当你只需要集合中的元素或索引时。
  2. 遍历字符串:
    • 当你需要逐字符处理字符串时,尤其是包含多字节字符的字符串。
    • 当你需要获取每个字符的字节索引时。
  3. 遍历映射:
    • 当你需要访问映射中每个键值对时。
    • 当你只需要映射的键或值时。
  4. 遍历通道:
    • 当你需要从通道中接收数据直到通道关闭时。
    • 当你需要处理通道发送的每个数据项时。

3. 注意事项

  • 在遍历映射时,迭代顺序是随机的,每次运行程序时可能会得到不同的顺序。
  • 在遍历通道时,如果通道未关闭,range 会一直阻塞等待数据。
  • 对于字符串,使用 range 遍历可以正确处理 Unicode 字符,包括多字节字符。

4.示例

4.1 遍历数组和切片
go 复制代码
func RangeDemo1() {
	nums := []int{1, 2, 3, 4, 5}
	for index, value := range nums {
		fmt.Println("数组遍历:", index, value)
	}

	for index, _ := range nums {
		//for index := range nums {
		fmt.Println("数组索引遍历:", index)
	}

	for _, value := range nums {
		fmt.Println("数组值遍历:", value)
	}
}

测试方法

go 复制代码
func TestRangeDemo1(t *testing.T) {
	RangeDemo1()
}

输出结果

复制代码
=== RUN   TestRangeDemo1
数组遍历: 0 1
数组遍历: 1 2
数组遍历: 2 3
数组遍历: 3 4
数组遍历: 4 5
数组索引遍历: 0
数组索引遍历: 1
数组索引遍历: 2
数组索引遍历: 3
数组索引遍历: 4
数组值遍历: 1
数组值遍历: 2
数组值遍历: 3
数组值遍历: 4
数组值遍历: 5
--- PASS: TestRangeDemo1 (0.00s)
PASS
4.2 遍历字符串
go 复制代码
func RangeDemo2() {
	str := "Hello, 世界杯"
	for index, runeValue := range str {
		fmt.Printf("%#U starts at byte position %d\n", runeValue, index)
	}

	for _, runeValue := range str {
		fmt.Printf("%#U starts at byte \n", runeValue)
	}
}

测试方法

go 复制代码
func TestRangeDemo2(t *testing.T) {
	RangeDemo2()
}

输出结果

复制代码
=== RUN   TestRangeDemo2
U+0048 'H' starts at byte position 0
U+0065 'e' starts at byte position 1
U+006C 'l' starts at byte position 2
U+006C 'l' starts at byte position 3
U+006F 'o' starts at byte position 4
U+002C ',' starts at byte position 5
U+0020 ' ' starts at byte position 6
U+4E16 '世' starts at byte position 7
U+754C '界' starts at byte position 10
U+676F '杯' starts at byte position 13
U+0048 'H' starts at byte 
U+0065 'e' starts at byte 
U+006C 'l' starts at byte 
U+006C 'l' starts at byte 
U+006F 'o' starts at byte 
U+002C ',' starts at byte 
U+0020 ' ' starts at byte 
U+4E16 '世' starts at byte 
U+754C '界' starts at byte 
U+676F '杯' starts at byte 
--- PASS: TestRangeDemo2 (0.00s)
PASS
4.3 遍历Map
go 复制代码
func RangeDemo3() {
	myMap := map[string]int{"a": 1, "b": 2, "c": 3}
	for key, value := range myMap {
		fmt.Println(key, value)
	}
}

测试方法

go 复制代码
func TestRangeDemo3(t *testing.T) {
	RangeDemo3()
}

输出结果

复制代码
=== RUN   TestRangeDemo3
b 2
c 3
a 1
--- PASS: TestRangeDemo3 (0.00s)
PASS
4.4 遍历通道
go 复制代码
func RangeDemo4() {
	ch := make(chan int, 5)
	ch <- 1
	ch <- 2
	ch <- 3
	close(ch)

	for value := range ch {
		fmt.Println(value)
	}
}

测试方法

go 复制代码
func TestRangeDemo4(t *testing.T) {
	RangeDemo4()
}

输出结果

复制代码
=== RUN   TestRangeDemo4
1
2
3
--- PASS: TestRangeDemo4 (0.00s)
PASS

源码

go 复制代码
// range_demo.go 文件

package range_demo

import "fmt"

func RangeDemo1() {
	nums := []int{1, 2, 3, 4, 5}
	for index, value := range nums {
		fmt.Println("数组遍历:", index, value)
	}

	for index, _ := range nums {
		//for index := range nums {
		fmt.Println("数组索引遍历:", index)
	}

	for _, value := range nums {
		fmt.Println("数组值遍历:", value)
	}
}

func RangeDemo2() {
	str := "Hello, 世界杯"
	for index, runeValue := range str {
		fmt.Printf("%#U starts at byte position %d\n", runeValue, index)
	}

	for _, runeValue := range str {
		fmt.Printf("%#U starts at byte \n", runeValue)
	}
}

func RangeDemo3() {
	myMap := map[string]int{"a": 1, "b": 2, "c": 3}
	for key, value := range myMap {
		fmt.Println(key, value)
	}
}

func RangeDemo4() {
	ch := make(chan int, 5)
	ch <- 1
	ch <- 2
	ch <- 3
	close(ch)

	for value := range ch {
		fmt.Println(value)
	}
}
go 复制代码
// range_demo_test.go 文件

package range_demo

import "testing"

func TestRangeDemo1(t *testing.T) {
	RangeDemo1()
}

func TestRangeDemo2(t *testing.T) {
	RangeDemo2()
}

func TestRangeDemo3(t *testing.T) {
	RangeDemo3()
}

func TestRangeDemo4(t *testing.T) {
	RangeDemo4()
}
相关推荐
2021_fc18 小时前
Flink笔记
大数据·笔记·flink
黑客思维者19 小时前
机器学习006:监督学习【回归算法】(概论)--教AI从历史中预测未来
人工智能·学习·机器学习·监督学习·回归算法
xunyan623419 小时前
面向对象(下)-内部类的分类
java·学习
UVM_ERROR19 小时前
RDMA Scheduler + TX + Completion RTL 开发经验分享
笔记·vscode·ssh·github·芯片
黑客思维者20 小时前
机器学习003:无监督学习(概论)--机器如何学会“自己整理房间”
人工智能·学习·机器学习·无监督学习
Vizio<20 小时前
STM32HAL库开发笔记-GPIO输入
笔记·stm32·单片机·嵌入式硬件
chinalihuanyu20 小时前
蓝牙开发笔记(BlueTooth,BLE,CH592)
笔记
其美杰布-富贵-李20 小时前
tsai 中 Learner 机制深度学习笔记
人工智能·笔记·深度学习
wdfk_prog20 小时前
[Linux]学习笔记系列 -- [fs]dcache
linux·数据库·笔记·学习·ubuntu
小智RE0-走在路上21 小时前
Python学习笔记(7)--集合,字典,数据容器总结
笔记·python·学习