Golang学习笔记_12------结构体
Golang学习笔记_13------数组
Golang学习笔记_14------切片
文章目录
range
1. 介绍
在Go语言中,range 关键字用于遍历数组、切片、字符串、映射(map)、通道(channel)等数据结构。
它提供了一种简洁且强大的方式来迭代这些集合中的元素。
2. 适用场景
- 遍历数组和切片:
- 当你需要访问集合中每个元素及其索引时。
 - 当你只需要集合中的元素或索引时。
 
 - 遍历字符串:
- 当你需要逐字符处理字符串时,尤其是包含多字节字符的字符串。
 - 当你需要获取每个字符的字节索引时。
 
 - 遍历映射:
- 当你需要访问映射中每个键值对时。
 - 当你只需要映射的键或值时。
 
 - 遍历通道:
- 当你需要从通道中接收数据直到通道关闭时。
 - 当你需要处理通道发送的每个数据项时。
 
 
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()
}