YouTip LogoYouTip

Go Range

In Go, the `range` keyword is used in `for` loops to iterate over the elements of an array, slice, channel, or map. For arrays and slices, it returns the index and the corresponding value at that index. For maps, it returns key-value pairs. The `range` format for a `for` loop can iterate over slices, maps, arrays, strings, and more. The format is as follows: for key, value := range oldMap { newMap = value } In the code above, `key` and `value` can be omitted. If you only want to read the key, the format is: for key := range oldMap Or like this: for key, _ := range oldMap If you only want to read the value, the format is: for _, value := range oldMap * * * ## Examples ### Arrays and Slices Traversing a simple slice, where 2**%d represents the exponent of 2: ## Example package main import "fmt" // Declare a slice containing powers of 2 var pow = []int{1, 2, 4, 8, 16, 32, 64, 128} func main() { // Iterate over the pow slice, i is the index, v is the value for i, v := range pow { // Print 2 to the power of i equals v fmt.Printf("2**%d = %dn", i, v) } } The output of the above example is: 2**0 = 1 2**1 = 2 2**2 = 4 2**3 = 8 2**4 = 16 2**5 = 32 2**6 = 64 2**7 = 128 ### Strings When `range` iterates over a string, it returns the index and the Unicode code point (rune) of each character. ## Example package main import "fmt" func main() { for i, c := range "hello" { fmt.Printf("index: %d, char: %cn", i, c) } } The output of the above example is: index: 0, char: h index: 1, char: e index: 2, char: l index: 3, char: l index: 4, char: o ### Map The `range` format for a `for` loop can omit the key and value, as shown in the following example: ## Example package main import "fmt" func main() { // Create an empty map with int keys and float32 values map1 := make(mapfloat32) // Add key-value pairs to map1 map1 = 1.0 map1 = 2.0 map1 = 3.0 map1 = 4.0 // Iterate over map1, reading key and value for key, value := range map1 { // Print key and value fmt.Printf("key is: %d - value is: %fn", key, value) } // Iterate over map1, reading only the key for key := range map1 { // Print key fmt.Printf("key is: %dn", key) } // Iterate over map1, reading only the value for _, value := range map1 { // Print value fmt.Printf("value is: %fn", value) } } The output of the above example is: key is: 4 - value is: 4.000000 key is: 1 - value is: 1.000000 key is: 2 - value is: 2.000000 key is: 3 - value is: 3.000000 key is: 1 key is: 2 key is: 3 key is: 4 value is: 1.000000 value is: 2.000000 value is: 3.000000 value is: 4.000000 ### Channel `range` iterates over the values received from a channel until the channel is closed. ## Example package main import "fmt" func main() { ch := make(chan int, 2) ch <- 1 ch %sn", k, v) } // range can also enumerate Unicode strings. The first parameter is the index of the character, and the second is the character itself (the Unicode value). for i, c := range "go" { fmt.Println(i, c) } } The output of the above example is: sum: 9 index: 1 a -> apple b -> banana 0 103 1 111
← Go SliceGo Passing Pointers To Functio β†’