github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/container/ring/example_test.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ring_test 6 7 import ( 8 "container/ring" 9 "fmt" 10 ) 11 12 func ExampleRing_Len() { 13 // Create a new ring of size 4 14 r := ring.New(4) 15 16 // Print out its length 17 fmt.Println(r.Len()) 18 19 // Output: 20 // 4 21 } 22 23 func ExampleRing_Next() { 24 // Create a new ring of size 5 25 r := ring.New(5) 26 27 // Get the length of the ring 28 n := r.Len() 29 30 // Initialize the ring with some integer values 31 // 初始化赋值 32 for i := 0; i < n; i++ { 33 r.Value = i 34 r = r.Next() 35 } 36 37 // Iterate through the ring and print its contents 38 // 迭代使用ring中的值 39 for j := 0; j < n; j++ { 40 fmt.Println(r.Value) 41 r = r.Next() 42 } 43 44 // Output: 45 // 0 46 // 1 47 // 2 48 // 3 49 // 4 50 } 51 52 func ExampleRing_Prev() { 53 // Create a new ring of size 5 54 r := ring.New(5) 55 56 // Get the length of the ring 57 n := r.Len() 58 59 // Initialize the ring with some integer values 60 for i := 0; i < n; i++ { 61 r.Value = i 62 r = r.Next() 63 } 64 65 // Iterate through the ring backwards and print its contents 66 // 反向打印这些值 67 for j := 0; j < n; j++ { 68 r = r.Prev() 69 fmt.Println(r.Value) 70 } 71 72 // Output: 73 // 4 74 // 3 75 // 2 76 // 1 77 // 0 78 } 79 80 func ExampleRing_Do() { 81 // Create a new ring of size 5 82 r := ring.New(5) 83 84 // Get the length of the ring 85 n := r.Len() 86 87 // Initialize the ring with some integer values 88 for i := 0; i < n; i++ { 89 r.Value = i 90 r = r.Next() 91 } 92 93 // Iterate through the ring and print its contents 94 // r.Do() 方法遍历 95 r.Do(func(p interface{}) { 96 fmt.Println(p.(int)) 97 }) 98 99 // Output: 100 // 0 101 // 1 102 // 2 103 // 3 104 // 4 105 } 106 107 func ExampleRing_Move() { 108 // Create a new ring of size 5 109 r := ring.New(5) 110 111 // Get the length of the ring 112 n := r.Len() 113 114 // Initialize the ring with some integer values 115 for i := 0; i < n; i++ { 116 r.Value = i 117 r = r.Next() 118 } 119 120 // Move the pointer forward by three steps 121 // 移动引用指针 122 r = r.Move(3) 123 124 // Iterate through the ring and print its contents 125 // 因为移动了引用指针, 所以当前读取值的位置就发生了变化 126 r.Do(func(p interface{}) { 127 fmt.Println(p.(int)) 128 }) 129 130 // Output: 131 // 3 132 // 4 133 // 0 134 // 1 135 // 2 136 } 137 138 func ExampleRing_Link() { 139 // Create two rings, r and s, of size 2 140 r := ring.New(2) 141 s := ring.New(2) 142 143 // Get the length of the ring 144 lr := r.Len() 145 ls := s.Len() 146 147 // Initialize r with 0s 148 for i := 0; i < lr; i++ { 149 r.Value = 0 150 r = r.Next() 151 } 152 153 // Initialize s with 1s 154 for j := 0; j < ls; j++ { 155 s.Value = 1 156 s = s.Next() 157 } 158 159 // Link ring r and ring s 160 // 将 r 和 s 连接成一个新的rs ring 161 rs := r.Link(s) 162 163 // Iterate through the combined ring and print its contents 164 rs.Do(func(p interface{}) { 165 fmt.Println(p.(int)) 166 }) 167 168 // Output: 169 // 0 170 // 0 171 // 1 172 // 1 173 } 174 175 func ExampleRing_Unlink() { 176 // Create a new ring of size 6 177 r := ring.New(6) 178 179 // Get the length of the ring 180 n := r.Len() 181 182 // Initialize the ring with some integer values 183 for i := 0; i < n; i++ { 184 r.Value = i 185 r = r.Next() 186 } 187 188 // Unlink three elements from r, starting from r.Next() 189 // 删除元素 190 r.Unlink(3) 191 192 // Iterate through the remaining ring and print its contents 193 r.Do(func(p interface{}) { 194 fmt.Println(p.(int)) 195 }) 196 197 // Output: 198 // 0 199 // 4 200 // 5 201 }