-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_range.go
More file actions
40 lines (34 loc) · 919 Bytes
/
11_range.go
File metadata and controls
40 lines (34 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package gobyexample
import "fmt"
// WorkWithRanges - create some ranges to iterate over elements in a
// variety of data structures
func WorkWithRanges() {
// use `range` to sum numbers in a slice
nums := []int{2, 3, 4}
sum := 0
for _, num := range nums {
sum += num
}
fmt.Println("sum:", sum)
// using index of element provided by `range`
for i, num := range nums {
if num == 3 {
fmt.Println("index:", i)
}
}
// `range` on map iterates over key/value pairs
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {
fmt.Printf("%s -> %s\n", k, v)
}
// `range` can also iterate over just the keys of map
for k := range kvs {
fmt.Println("key:", k)
}
// `range`` on strings iterates over Unicode code points.
// The first value is the starting byte index of the rune,
// and the second value is the rune itself.
for i, c := range "go" {
fmt.Println(i, c)
}
}