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()
}
相关推荐
Bechamz12 小时前
大数据开发学习Day24
大数据·学习
在下_诸葛12 小时前
langgraph学习笔记
笔记·python·学习·langgraph教程
yue20040312 小时前
Spring IoC 与 DI 核心概念与原理笔记
java·笔记·spring
开心码农1号12 小时前
Go 语言深度剖析:指针、unsafe.Pointer 与 uintptr 底层原理、区别与实战避坑
开发语言·后端·golang
charlie11451419113 小时前
现代Qt开发教程(新手篇)1.10——进程
开发语言·c++·qt·学习
绿豆人13 小时前
Cache缓存项目学习2
学习·缓存
山楂树の13 小时前
H.265 (HEVC) 视频解码转逐帧图像 完整实现方案
学习·音视频·h.265
星幻元宇VR13 小时前
VR观景台推动安全科普走向沉浸体验
科技·学习·安全·vr·虚拟现实
码途漫谈13 小时前
Easy-Vibe开发篇阅读笔记(十三)——附录之用 Dify 搭建知识库问答系统
笔记·ai·开源·ai编程
十安_数学好题速析13 小时前
【多选】成比之道:巧解三角形中比例综合
笔记·学习·高考