Go range
last modified April 11, 2024
In this article we show how to iterate over data structures in Golang.
The Go for range form can be used to iterate over strings, arrays, slices, maps, and channels.
$ go version go version go1.22.2 linux/amd64
We use Go version 1.22.2.
Go range array
The following example uses range to iterate over a Go array.
package main import "fmt" func main() { vals := [...]int{5, 4, 3, 2, 1} for idx, e := range vals { fmt.Println("element", e, "at index", idx) } }
We iterate over an array of integer values.
$ go run array_range.go element 5 at index 0 element 4 at index 1 element 3 at index 2 element 2 at index 3 element 1 at index 4
package main import "fmt" func main() { vals := [...]int{5, 4, 3, 2, 1} for _, e := range vals { fmt.Printf("%d ", e) } fmt.Println("\n******************") for idx, _ := range vals { fmt.Printf("%d ", idx) } fmt.Println("\n******************") for idx := range vals { fmt.Printf("%d -> %d\n", idx, vals[idx]) } }
This example shows that the index or the element in the
iteration can be skipped by using _
.
for idx := range vals { fmt.Printf("%d -> %d\n", idx, vals[idx]) }
In this form, we are iterating over indexes.
Go range map
The following example uses range to iterate over a Go map.
package main import "fmt" func main() { data := map[string]string{ "de": "Germany", "it": "Italy", "sk": "Slovakia", } for k, v := range data { fmt.Println(k, "=>", v) } fmt.Println("----------------------") for k := range data { fmt.Println(k, "=>", data[k]) } }
The example iterates over a range of country map, where the keys and the values are strings.
$ go run map_range.go de => Germany it => Italy sk => Slovakia ---------------------- de => Germany it => Italy sk => Slovakia
Go range string
The following example uses range to iterate over a Go string.
package main import "fmt" func main() { s := "合気道" for idx, e := range s { fmt.Printf("%d %c\n", idx, e) } fmt.Println() }
In the code example, we iterate over Go runes.
$ go run string_range.go 0 合 3 気 6 道
This is the output. Each of the runes has three bytes.
Go range channel
The following example uses range to iterate over a Go channel. A channel is a pipe through which goroutines communicate; the communication is lock-free.
package main import "fmt" func main() { ch := make(chan int) go func() { ch <- 5 ch <- 6 ch <- 7 close(ch) }() for n := range ch { fmt.Println(n) } }
In the code example, we iterave over values sent through the channel.
$ go run channel_range.go 5 6 7
Source
The Go Programming Language Specification
In this article we have covered for range forms in Golang.
Author
List all Go tutorials.