Skip first element of an array in Golang
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
}
convert to slice then skip first element(with the range query):
package main
import "fmt"
func main() {
Arr := [...]int{1, 2, 3, 4}
for _, val := range Arr[1:] {
fmt.Println(val)
}
}
using index to skip first element(with the range query):
package main
import "fmt"
func main() {
Arr := [...]int{1, 2, 3, 4}
for i, val := range Arr {
if i == 0 {
continue
}
fmt.Println(val)
}
}
using one bool var to skip first element(with the range query):
package main
import "fmt"
func main() {
Arr := [...]int{1, 2, 3, 4}
first := true
for _, val := range Arr {
if first {
first = false
continue
}
fmt.Println(val)
}
}