Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate on a golang array/slice without using for statement

Tags:

arrays

go

Is it possible to iterate on a golang array/slice without using 'for' statement?

like image 571
nathanos Avatar asked Dec 05 '25 00:12

nathanos


1 Answers

You could use goto statement (not recommended).

package main

import (
    "fmt"
)

func main() {
    my_slice := []string {"a", "b", "c", "d"}

    index := 0

back:
    if index < len(my_slice) {
        fmt.Println(my_slice[index])
        index += 1
        goto back
    }
}
like image 57
Akavall Avatar answered Dec 07 '25 17:12

Akavall