The range keyword is mainly used for loops to iterate over all the elements of a map, slice, channel, or array.
package main import "fmt" var Prwa = []int{1, 3, 9, 27, 81, 243, 729, 2187} func main() { for i, x := range Prwa { fmt.Printf("3**%d=%d\n", i, x) } }
Output :
PS C:\GO_Language\range> go run range.go 3**0=1 3**1=3 3**2=9 3**3=27 3**4=81 3**5=243 3**6=729 3**7=2187
- Initializing slices using Range.
package main import "fmt" func main() { pow := make([]int, 10) for i:= range pow { pow[i] = i
Output :
PS C:\GO_Language\range> go run range2.go 0 2 8 24 64 160 384 896 2048 4608
- Simple Map program
package main import "fmt" func main() { var map_1 map[int]int if map_1 == nil { fmt.Println("True") } else { fmt.Println("False") } map_2 := map[int]string{ 90: "Red", 91: "Yellow", 92: "Orange", 93: "Green", 94: "Pink", 95: "Blue", 96: " ", } fmt.Println("Map_2", map_2) }
Output :
PS C:\GO_Language\range> go run range3.go True Map_2 map[90:Red 91:Yellow 92:Orange 93:Green 94:Pink 95:Blue 96: ]
The post Range and mapping in Golang appeared first on Prwatech.
This post first appeared on Learn Big Data Hadoop In Bangalore, please read the originial post: here