Golang Range
Golang Range
Golang Range
In Golang, range keyword is used in conjunction with for loop to iterate over the items of a collection.
Range can be used with an array, string, map or channels. During each iteration, range can return one or two values based on the type of collection you are iterating. Following table, provides an overview of what range returns during iteration.
Range Expression 1st Value 2nd Value
Array or slice String Map Channel
index index key element
element rune int (int32 value representing the character value) value none
Using second value returned by the range is optional. Going forward in this article, we will learn how to use golang range with different range expressions mentioned in the above table.
Golang Range with Array
In the following examples, we will take an array and iterate over the elements using for loop and range.
Example 1 : Golang Range with Array
In this example, we will use index only returned from range over array to iterate over the elements of array.
example.go
package main import "fmt" func main() {
var evens = [5] int {2, 4, 6, 8, 10} // using index returned from range
// using index returned from range for index:= range evens {
fmt.Printf("evens[%d] = %d \n", index, evens[index]) } }
Output
evens[0] = 2 evens[1] = 4 evens[2] = 6 evens[3] = 8 evens[4] = 10
Example 2 ? Index and Element
In this example, we will use both index and element returned from Range over Array.
example.go
package main import "fmt" func main() {
var evens = [5] int {2, 4, 6, 8, 10} // using index and element returned from range for index,element:= range evens {
fmt.Printf("evens[%d] = %d \n", index, element) } }
Output
evens[0] = 2 evens[1] = 4 evens[2] = 6 evens[3] = 8 evens[4] = 10
Golang Range with String
In the following examples, we will take a String and iterate over the characters of it, using for loop and range.
Example 1 ? Range & String
In this example, we will iterate over characters of a given string using range.
example.go
package main import "fmt" func main() {
var str = "Golang" // using index returned from range for index:= range str {
fmt.Printf("str[%d] = %d \n", index, str[index]) } }
Output
str[0] = 71 str[1] = 111 str[2] = 108 str[3] = 97 str[4] = 110 str[5] = 103
The element is the rune, which is the int32 ASCII value of the character.
Example 2: Range & String
In this example, we will use range to get both the index and character during each iteration over a string.
example.go
package main import "fmt" func main() {
var str = "Golang" // using index and element returned from range for index,element:= range str {
fmt.Printf("str[%d] = %d \n", index, element) } }
Output
str[0] = 71 str[1] = 111 str[2] = 108 str[3] = 97
str[3] = 97 str[4] = 110 str[5] = 103
Golang Range with Map
In the following examples, we will take a Map and iterate over the key:value pairs of it, using for loop and range.
Example 1 ? Range & Map
In this example, we will iterate over elements of a given Golang Map with index.
example.go
package main import "fmt" func main() {
var colorMap = make(map[string]string) colorMap["white"] = "#FFFFFF" colorMap["black"] = "#000000" colorMap["red"] = "#FF0000" colorMap["blue"] = "#0000FF" colorMap["green"] = "#00FF00" for color := range colorMap {
fmt.Println("Hex value of", color, "is", colorMap[color]) } }
Output
Hex value of white is #FFFFFF Hex value of black is #000000 Hex value of red is #FF0000 Hex value of blue is #0000FF Hex value of green is #00FF00
The element is the rune, which is the int32 ASCII value of the character.
Example 2 ? Key-Value from Range over Map
In this example, we will iterate over elements of a given Golang Map. During an iteration, we get both key and value using range.
example.go
package main import "fmt" func main() {
var colorMap = make(map[string]string) colorMap["white"] = "#FFFFFF" colorMap["black"] = "#000000" colorMap["red"] = "#FF0000" colorMap["blue"] = "#0000FF" colorMap["green"] = "#00FF00" for color, hex := range colorMap {
fmt.Println("Hex value of", color, "is", hex) } }
Output
Hex value of white is #FFFFFF Hex value of black is #000000 Hex value of red is #FF0000 Hex value of blue is #0000FF Hex value of green is #00FF00
Conclusion
In this Golang Tutorial, we learned about Ranges in Go language and how to use them with the help of example programs.
Golang Golang Tutorial Run Golang Program Golang If Else Golang Switch Golang For Loop Golang Comments Golang Functions Golang Array Golang Slice Golang Struct Golang Class
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- how to avoid go gotchas
- the go programming language part 1
- francesc why and how to write good go code an cern
- agolangwrapperformpi vi4io
- golang range
- develop your embedded applications faster comparing c and
- golang cheat sheet
- go programming language
- designing and building a distributed data store in go
- go 学习笔记 第六版 下卷