Is there a shorter form for Go for ranges

I checked the language specification - is there an easier way to say this:

for _, month := range []int{4,6,9,11} {
    fmt.Print(month, " ")
}

I (ideally) looked for something like (I know this is not Go)

for month in [4,6,9,11] {
    fmt.Print(month, " ")
}

I know that I can:

days30 := []int{4,6,9,11} 
for i := range days30 {
    fmt.Print(days30[i], " ")
}

But this is less readable ...

Note. This is for learning — so I’m looking for a simple stand-alone solution for students — nothing too advanced.

+4
source share
2 answers

No.

+19
source

The short answer is no. Using a range on a slice will always either give an index, or an index, a value.

+1
source

All Articles