create a range with a start in go

To create a range with a specific starting point in golang, we can simply use the built-in range keyword followed by our starting point, a colon, and the length of our range. Here's an example code block:

main.go
package main

import "fmt"

func main() {
    start := 3
    length := 4

    for i := start; i < start+length; i++ {
        fmt.Println(i)
    }
}
149 chars
13 lines

In this example, our starting point is 3 and our range length is 4. We initialize our for loop with i equal to start and we iterate until i is equal to start+length. This will output:

main.go
3
4
5
6
8 chars
5 lines

Note that we can also use range on slices, maps, and channels in golang.

related categories

gistlibby LogSnag