-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.go
More file actions
39 lines (32 loc) · 732 Bytes
/
Copy pathrange.go
File metadata and controls
39 lines (32 loc) · 732 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
package main
import "fmt"
func main() {
// Sum the contents of a slice
nums := []int{2, 3, 4}
sum := 0
for _, num := range nums {
sum += num
}
fmt.Println("sum:", sum)
// These auto enumerate with index value
for i, num := range nums {
if num == 3 {
fmt.Println("index:", i)
}
}
// Iterate over the key pairs
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {
fmt.Printf("%s -> %s\n", k, v)
}
// Iterate over just the keys
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 the rune itself
for i, c := range "go" {
fmt.Println(i, c)
}
}