liveBook · Manning

Loop

As explained in Go in Action, Second Edition

A for loop is a Go control-flow construct that repeatedly executes a block according to optional initialization and post clauses and a loop condition or range clause.

A for loop repeatedly executes a block while controlling initialization, continuation, and progression. Go unifies looping under the for statement rather than providing separate while and do-while constructs. section 2.4.10

The three-part form contains optional initialization and post clauses around a required condition:

  • The init statement runs once, usually creating a counter. section 2.4.10
  • The Boolean condition is checked before every iteration; Go requires it in this form. section 2.4.10
  • The post statement runs after each iteration, including the final one, and commonly increments the counter. section 2.4.10
  • A range clause iterates through arrays with indices, values, or both; returned values are copies, so direct mutation requires an index or pointers. section 4.1.4

Counting spaces

A counter-based loop examines each character position and increments numSpaces when text[i] is a space. This pattern combines indexing, a loop condition, and a post increment. section 2.4.10

Processing streamed input

A scanner loop continues while scanner.Scan() succeeds, processes scanner.Text() on each iteration, and checks scanner.Err() afterward. A word-counting variant applies strings.Fields per scanned line and accumulates the result. section 2.8.1

Listing 2.14 Looping over string characters with a for loop
for i := 0; i < len(text); i++ {
	if text[i] == ' ' {
		numSpaces++
	}
}

Inspect the complete character-scanning loop used to count spaces. section 2.4.10

Listing 4.11 Example loops
for i := range items {
	// do something with items[i]
}

for i, item := range items {
	// do something with item or items[i]
}

for i := 0; i < len(items); i++{
	// do something with items[i]
}

Compare range iteration with index-based traversal. section 4.1.4

Listing 2.38 Example usage from go doc -ex bufio.NewScanner
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
	fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
	fmt.Fprintln(os.Stderr, "reading standard input:", err)
}

See the idiomatic Scanner loop and its post-loop error check. section 2.8.1

How Go in Action, Second Edition covers it

How deep it goes
Explains three-part execution, range value-copy behavior, indexed mutation, and Scanner termination, but defers broader loop forms.
Examples
Demonstrates character scanning, array traversal, reverse iteration, skipped elements, and line-by-line word counting.
What it assumes
Assumes familiarity with C-like loop conventions while teaching Go's unified for syntax and range behavior.
What you can do after
Apply for loops to scan data, traverse collections, mutate indexed elements, and process Scanner input safely.
How it connects
Connects for loops with range clauses, arrays, slices, indices, pointers, Scanner, strings.Fields, and error handling.

Also explained in

Compare how 27 books explain loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.