...

Web Technologies - Go Language

Back to Course

Lesson Description


Lession - #365 Go-Range


Go - Range

The range keyword is utilized in for circle to emphasize over things of an exhibit, slice, channel or guide. With exhibit and slice, it returns the file of the thing as whole number. With maps, it returns the key of the following key-esteem pair. Range either returns one worth or two. If by some stroke of good luck one worth is utilized on the left of a reach expression.
Example

package main

import "fmt"

func main(>
{ /* create a slice */ numbers := []int{0,1,2,3,4,5,6,7,8} /* print the numbers */ for i:= range numbers { fmt.Println("Slice item",i,"is",numbers[i]>
} /* create a map*/ countryCapitalMap := map[string] string {"France":"Paris","Italy":"Rome","Japan":"Tokyo"} /* print map using keys*/ for country := range countryCapitalMap { fmt.Println("Capital of",country,"is",countryCapitalMap[country]>
} /* print map using key-value*/ for country,capital := range countryCapitalMap { fmt.Println("Capital of",country,"is",capital>
} }