HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GoVariablesData TypesOperatorsStringsArray & SliceMap (Dictionary)Conditions (if / switch)Loops (for)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Slice
🗂️ Map
🔁 Condition & Loop
🧩 Function
Go›Loops (for)

Loops (for)

Go's only loop is for, with three forms: classic, conditional (like while), and range.

Simple enough for a 5-year-old to understand

Classic for

Example code
1
package main
2
import "fmt"
3
func main() {
4
for i := 1; i <= 3; i++ {
Initialization; condition; increment.
LoopInitialization; condition; increment.
5
fmt.Println(i)
1, 2, 3
Output / Input1, 2, 3
6
}
7
}
LoopOutput / Input
💡 Hint: Go only has for; while is also written with for: for condition { }.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the numbers from 1 to 5 vertically using a for loop.

🎯 Expected output: 1 2 3 4 5

Loading editor…

About this topic

There is a single loop keyword in Go: for. There is no while, do-while, or foreach — all of them are written using different forms of for. This is a simplification that noticeably makes the language easier to learn.

There are four forms: the classic three-part for, the conditional-only for (equivalent to while), the partless for (infinite loop), and iterating with range. range works on slices, maps, strings, and channels, and is the form you will see most often in Go code.

There is an important detail about range: it gives a COPY of the element. If you want to modify slice elements inside the loop, you need to access them by index, otherwise the changes will be lost.

What you will learn

  • the four forms of for
  • iterating over slices, maps, and strings with range
  • getting the index and value separately
  • Using break and continue
  • breaking out of nested loops with labeled break
  • that range gives a copy

When to use?

range is the default way to iterate over collections. If you only need the index, you can discard the value with an underscore; if you only need the value, you discard the index with an underscore.

The partless infinite for is used for server loops and jobs waiting on channels. In Go, this form is written instead of while.

Common mistakes

Wrong

nums := []int{1, 2, 3}
for _, n := range nums {
	n = n * 2
}
fmt.Println(nums)

range gives a COPY of the element; changing n does not affect the slice, and the output remains unchanged. To modify the slice, you must access it by index.

Correct

nums := []int{1, 2, 3}
for i := range nums {
	nums[i] = nums[i] * 2
}
fmt.Println(nums)

Wrong

i := 0
for i < 3 {
	fmt.Println(i)
}

Since the counter is not incremented, the condition always remains true and the program enters an infinite loop. In Go, a conditional for is used instead of while, but the responsibility of incrementing the counter is still yours.

Correct

i := 0
for i < 3 {
	fmt.Println(i)
	i++
}

Wrong

for i := 0; i < 3; i++ {
	for j := 0; j < 3; j++ {
		if j == 1 {
			break
		}
	}
}

break only breaks the INNER loop; the outer loop continues to run. To break out of both loops at once, a labeled break is needed — this is supported in Go and is a readable solution.

Correct

outer:
for i := 0; i < 3; i++ {
	for j := 0; j < 3; j++ {
		if j == 1 {
			break outer
		}
	}
}

Frequently asked questions

Why is there no while loop in Go?

It was deemed unnecessary: a for loop with all parts except the condition left empty behaves exactly like a while loop. Go was designed with the principle of not having multiple keywords that do the same thing.

How do I skip the index with range?

With an underscore. If you only need the value, you discard the index with an underscore; if you only need the index, the single-variable form already gives you the index. On a map, the first variable is the key.

Why did the loop variable cause problems in closures?

In older Go versions, the loop variable was SHARED every iteration, so when used inside a goroutine or closure, they all saw the final value. Since Go 1.22, a new variable is created every iteration and this classic bug is gone.

Summary

The only loop keyword in Go is for, and while and foreach are its forms. range is the default way to iterate over collections, but it gives a copy of the element — to modify the slice, you need to access it by index.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code