golang-- learn about the for loop

1, for cycle usage

(1) General usage

func main() {
	slice := []int{1, 2, 3, 4, 5, 6}
	//方式1
	for i := 0; i < len(slice); i++ {
		if i < 10 {
			slice = append(slice, 0)
		}
		fmt.Printf("%v ", slice[i]) //1 2 3 4 5 6 0 0 0 0 0 0 0 0 0 0
	}
	fmt.Println()
	//方式2
	slice = slice[:6]
	n := len(slice)
	for i := 0; i < n; i++ {
		fmt.Printf("%v ", slice[i]) //1 2 3 4 5 6
	}
	fmt.Println()
}

Embodiment 1 will be executed each cycle len function for dynamic (loop outlet is dynamic);

Mode 2 is suitable for fixed cycle (typically a fixed number of cycles), the fixed cycle is not recommended to use (if circulating outlet is a time-consuming operation, will affect the efficiency) 1; 

(2) an infinite loop

func main() {
	slice := []int{1, 2, 3, 4, 5, 6}
	for {
		slice = append(slice, 0)
		if len(slice) > 10 {
			break
		}
	}
	fmt.Println(slice) //[1 2 3 4 5 6 0 0 0 0 0]
}

Endless loop is generally used to monitor the operation, or exit the cycle is not unique, the need to break with the end of the cycle.

(3)for...range

func main() {
	slice := []int{1, 2, 3, 4, 5, 6}
	for i := range slice {
		fmt.Printf("%d ", i) //0 1 2 3 4 5
	}
	fmt.Println()
	for i, v := range slice {
		fmt.Printf("slice[%d]=%d ", i, v)
		//slice[0]=1 slice[1]=2 slice[2]=3 slice[3]=4 slice[4]=5 slice[5]=6
	}
}

support for ... range iterative array, slice, string, map, read-only or read-write channel channel;

For ... range of different types of different return values ​​for the iteration, arrays, chips, and index value string is returned, map key and returns a value, the return value Channel;  

2, for the difference for range when traversing the string

func main() {
	str := "abcd笃志弘毅"
	n := len(str)
	for i := 0; i < n; i++ {
		fmt.Printf("%c", str[i]) //abcd笃志弘æ¯
	}
	fmt.Println()
	for i, v := range str {
		fmt.Printf("str[%d]=%c", i, v)
		//str[0]=astr[1]=bstr[2]=cstr[3]=dstr[4]=笃str[7]=志str[10]=弘str[13]=毅
	}
}

When the string is traversed for traversing byte (byte), byte code exceeds a garbled characters;

for ... When traversing range is traversed by a string of characters (rune);  

3, for ... range Precautions

(1) equivalent iterative slice sliced ​​a copy of the traverse, then the original slice append or slice does not affect the entire iteration

main FUNC () { 
	Slice: = [] {int. 1, 2,. 3,. 4,. 5,. 6} 
	for I, V: = Range Slice { 
		// iteration sections corresponds temp: = slice [:], temp traverse sections 
		// slice or append the re-sliced slice [1: 2] does not affect the entire iteration 
		slice = append (slice, 0) 
		slice slice = [0: len (slice)] 
		fmt.Printf ( "slice [D%] =% D ", I, V) 
		// Slice [0] = Slice. 1 [. 1] = 2 Slice [2] Slice. 3 = [. 3]. 4 Slice = [. 4] Slice. 5 = [. 5] =. 6 
	} 
	fmt.Println (" \ n-", Slice) // [. 1. 5. 4. 3 2. 6 0 0 0 0 0 0] 
}

(2) the value type iterative changes do not affect the original data, pointer type iterations changes will affect the original data

func main() {
	//切片
	slice := []int{1, 2, 3, 4, 5, 6}
	for _, v := range slice {
		v += v
	}
	fmt.Println(slice) //[1 2 3 4 5 6]
	//map
	m := map[int]*int{}
	m[1] = new(int)
	m[2] = new(int)
	*m[1] = 1
	*m[2] = 2
	fmt.Println(*m[1], *m[2]) //1 2
	for _, v := range m {
		*v++
	}
	fmt.Println(*m[1], *m[2]) //2 3
}

(3) for ... range iteration map

"Access is random, through the results may be different;

func main() {
	m := map[int]int{0: 0, 1: 1, 2: 2, 3: 3, 4: 4}
	for k, v := range m {
		fmt.Printf("%v %v,", k, v) //2 2,3 3,4 4,0 0,1 1,
	}
}

"Normal delete operation can be carried out, because iteration is a copy;

func main() {
	m := map[int]*int{}
	m[1] = new(int)
	m[2] = new(int)
	*m[1] = 1
	*m[2] = 2
	fmt.Println(*m[1], *m[2]) //1 2
	for k, v := range m {
		delete(m, k)
		*v++
		fmt.Printf("%v ", *v) //2 3
	}
}

"Add operation can be performed properly, since the map is the underlying hash table, the hash table does not have the elements of the sequence, the newly added elements may not appear in subsequent iterations;

func main() {
	m := map[int]string{1: "一", 2: "二"}
	for k, v := range m {
		m[k+2] = "测试"
		fmt.Println(k, v)
	}
	fmt.Println(m)
}

Guess you like

Origin www.cnblogs.com/dzhy/p/10977779.html