Back to all posts
programming golang go

Go Loops

0 min read (92 words)

There is For loop in Go, pretty easy:)

while True = just for

    for {
        fmt.Printf("Slice time:\n")
    }

For each in = range (similar to enumerate() in Python) is used to go over slices or maps

    a := []string{"a1", "b1", "b1", "b1", "b1", "b1"}
    for index, value := range a {
        fmt.Printf("Index %v: %vn", index, value)
    }
Go code examples showing basic for loop and infinite loop syntax

Use _ if you don't need to use index or value

    a := []string{"a1", "b1", "b1", "b1", "b1", "b1"}
    for _, value := range a {
        fmt.Printf("Value: %vn", value)
    }

IF ELSE is the same

Go code showing range-based loops and underscore usage for unused variables
Dmitry Golovach
About

Dmitry Golovach

Principal Network Engineer and AI enthusiast. Always learning, always building.

Share this post

All posts