I have the following code segments
for _, val := range Arr {
// something have to do with val
}
In Arr , there might be more than 1 elements. I want to skip the first element of Arr and continue the loop from the second element.
For example if Arr contains {1,2,3,4}. With the query I just want to take {2,3,4}.
Is there a way to do that with the range query ?
Yes. Use this
for _, val := range Arr[1:] {
// something to do with val
}
Or in case you can get empty slice:
for i := 1; i < len(Arr); i++ {
// something to do with Arr[i]
}
Use a standard for
loop or the slice operator:
for _, val := range Arr[1:] {
// Do something
}
// Or
for i := 1; i < len(Arr); i++ {
val = Arr[i]
// Do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With