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()
}
相关推荐
霍霍的袁4 分钟前
【C++11】左值右值引用与移动语义详细笔记(左值右值、右值引用、移动构造、移动赋值、引用折叠、万能转发)
c++·笔记·学习·visual studio
Agent世界5 分钟前
千问Go上线我的应用:一句话生成可部署 AI 软件的技术拆解
开发语言·人工智能·golang·ai智能体
MandalaO_O5 分钟前
SpringMVC 异常处理 & 拦截器
笔记
外收内放16 分钟前
FastAPI猜字谜实战(6):日志与异常
开发语言·python·学习·fastapi
知识分享小能手1 小时前
C学习教程,从入门到精通,C语言单片机基础 — 完整知识点手册(19)
c语言·单片机·学习
小黄人软件1 小时前
Cluade 桌面端配置(ccswitch配置或文件直接配置)
学习
念何架构之路1 小时前
Go reflect包值操作 Value 的读写、可设置性与 interface 布局
开发语言·后端·golang
李永奉1 小时前
杰理可视化SDK开发-音频录制功能使用教程
笔记·单片机·嵌入式硬件·mcu·物联网·音视频
努力努力再努力FFF1 小时前
普通人适合考什么AI证书?不同基础和学习目标怎么选
人工智能·学习
ysu_03141 小时前
【C++ 进阶之路】(二)从内联函数到类与对象:深挖内存对齐、this 指针与三大默认成员函数
开发语言·数据结构·c++·笔记·面向对象·this