0% found this document useful (0 votes)
10 views2 pages

Go Infinite Loops Explained

The document discusses different types of loops in Go including for loops, infinite loops, and for-each loops to iterate over elements in arrays, slices, and other data structures. It provides examples of using range to iterate and optionally ignoring the key by using an underscore.

Uploaded by

Bahadur Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Go Infinite Loops Explained

The document discusses different types of loops in Go including for loops, infinite loops, and for-each loops to iterate over elements in arrays, slices, and other data structures. It provides examples of using range to iterate and optionally ignoring the key by using an underscore.

Uploaded by

Bahadur Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Loops

Loops are simplified in Go

You only have the “for loop”

Infinite Loop

for {
//anything inside will run infinitely
}
//same as

for true {
//anything inside will run infinitely
}

for-each loop
Iterating over a list
range

range iterates over elements for different data structures (so not only arrays and
slices)

for arrays and slices, range provides the index and value for each element

In Go, like for each loop, if you don’t want to use the <key>, you will
leave it blank. Then go will give error. To remove the error and not
use the <key>, use _ instead.

for <key>, <value> := range <container>{

Loops 1
//This can be written as
for _, <value> := range <container>{

Now go ignores the <key> and also does not gives error.

Loops 2

You might also like