foreach loop in Go

There’s no foreach loop in Golang.

However we can replicate foreach loop behaviour using for loop in Go.

A “for” statement with a “range” clause loops through all elements of an array, slice, string or map.

For-each element it assigns iteration values to corresponding iteration variables if present and then executes the block.

package main

import (
	"fmt"
)

func main() {
	stringSlice := []string{"Hello", "Golang"}
	for index, element := range stringSlice {
		fmt.Println(index, element)
	}
}

And the output of above foreach loop is

0 Hello
1 Golang

But that’s not exactly the replica of foreach loop, because it contains index.

We can use underscore(_) variable or blank identifier in Golang in place of index variable.

//A foreach loop in Go
for _, element := range stringSlice {
		fmt.Println(element)
	}
Avatar

Arunkumar Gudelli

I am One among a million Software engineers of India. I write beautiful markup.I make the Web useful.

Follow us @ twitter, facebook and linkedin For latest news and articles about Go Language

☝ ✍ Go Language Tutorial ✍☝
Get a short & sweet Go Language tutorials delivered to your inbox every couple of days. No spam ever. Unsubscribe any time.