-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.go
More file actions
34 lines (26 loc) · 679 Bytes
/
range.go
File metadata and controls
34 lines (26 loc) · 679 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
package main
import (
"fmt"
)
func RangeProgram() {
nums := []int{1, 2, 3, 4, 5}
for i := range len(nums) {
fmt.Print(i, " ")
}
fmt.Println()
// _ = index j = element
for _, j := range nums { // the first value given by range is the index but on inbuilt types like map, slices, arrays it will return the element as well
fmt.Print(j, " ")
}
fmt.Println()
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {
fmt.Printf("%s -> %s\n", k, v)
}
for k := range kvs { // here the first element is key and sec is value
fmt.Println("key:", k)
}
for i, c := range "go" {
fmt.Println(i, c)
}
}