Saturday, 26 August 2017

go - Is there a way to iterate over a range of integers?



Go's range can iterate over maps and slices, but I was wondering if there is a way to iterate over a range of numbers, something like this



for i := range [1..10] {
fmt.Println(i)
}



or is there a way to represent range of integers in Go like how ruby does?


Answer



You can, and should, just write a for loop. Simple, obvious code is the Go way.



for i := 1; i <= 10; i++ {
fmt.Println(i)
}

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...