Loops with for

Contents
Classic three-part for
for as while
Range over slices and maps
break and continue

Classic three-part for

Go has a single loop keyword: for. The classic form has three parts separated by semicolons: the setup, the condition checked before every step, and the update that runs after every step.

package main import "fmt" func main() { sum := 0 for i := 1; i <= 5; i++ { sum += i } fmt.Println(sum) }

go run main.go

15

The counter variable declared in the setup lives only inside the loop. Opening braces stay on the same line as the for header, exactly like if and switch.

for as while

Drop the three parts and for behaves like a while loop from other languages: it repeats while its single condition holds. This form fits readers, retries, and countdowns where no counter is needed.

package main import "fmt" func main() { n := 3 for n > 0 { fmt.Println(n) n-- } }

3
2
1

A bare for with no condition at all loops forever, so pair it with a break or a return that always becomes reachable.

Range over slices and maps

Range walks a slice or a map and hands you each element in turn. Over a slice you receive the index and a copy of the element; over a map you receive the key and the value.

package main import "fmt" func main() { nums := []int{1, 2, 3} for i, n := range nums { fmt.Println(i, n) } ages := map[string]int{"Ada": 36, "Bob": 28} for name, age := range ages { fmt.Println(name, age) } }

Map iteration order changes from run to run on purpose, so never rely on it. When order matters, collect the keys into a slice and sort that slice first.

break and continue

Break exits the loop at once, while continue skips the rest of the current step and jumps to the next one. Together they keep loop bodies flat by handling special cases first.

package main import "fmt" func main() { for i := 1; i <= 10; i++ { if i%2 == 0 { continue } if i > 7 { break } fmt.Println(i) } }

1
3
5
7

Here continue skips even numbers and break stops the loop once values grow past 7. Reading such loops from top to bottom shows each filter before the useful work at the end.

Next: Arrays

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: