How to generate a sequence of numbers

There is no equivalent to PHP’s range in the Go standard library. You have to create one yourself. The simplest is to use a for loop:

func makeRange(min, max int) []int {
    a := make([]int, max-min+1)
    for i := range a {
        a[i] = min + i
    }
    return a
}

Using it:

a := makeRange(10, 20)
fmt.Println(a)

Output (try it on the Go Playground):

[10 11 12 13 14 15 16 17 18 19 20]

Also note that if the range is small, you can use a composite literal:

a := []int{1, 2, 3}
fmt.Println(a) // Output is [1 2 3]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)