TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
for range loop is a control flow construct in Go used to iterate over built-in iterable data structures and iterator functions. It abstracts the boilerplate of traditional loops, yielding one or two iteration variables per step depending on the type of the iterable expression evaluated on the right side of the range keyword.
Base Syntax
The syntax allows declaring up to two iteration variables:Iteration Mechanics by Type
The arity (number of variables yielded) and the semantics of the iteration variables depend strictly on the type of theiterable.
- Arrays and Slices (Yields 2 variables):
first: Integer index (starting at0).second: A copy of the element at that index.
- Strings (Yields 2 variables):
first: The starting byte index of the current Unicode character.second: Arune(anint32representing the Unicode code point).- UTF-8 Semantics: Because characters can be multi-byte, the index may not increment sequentially by 1. If the loop encounters an invalid UTF-8 byte sequence, the
secondvariable yields the Unicode replacement character (\uFFFD) and thefirstvariable advances by exactly one byte.
- Maps (Yields 2 variables):
first: The map key.second: The map value.- Behavioral Note: Map iteration order is intentionally non-deterministic. Go randomizes the starting hash bucket to prevent reliance on map iteration order.
- Modification Rules: If a map entry is removed before it is reached during iteration, it will not be produced. If an entry is added during iteration, it may or may not be produced.
- Channels (Yields 1 variable):
first: The value received from the channel.- Behavioral Note: The loop blocks until a value is available and terminates only when the channel is closed and all values sent prior to the close have been received.
- Integers (Go 1.22+) (Yields 1 variable):
first: An integer incrementing from0up toiterable - 1.
- Functions / Iterators (Go 1.23+) (Yields 1 or 2 variables):
- Go supports ranging over any function that matches specific iterator signatures:
func(yield func(V) bool)(yields 1 variable, the value) orfunc(yield func(K, V) bool)(yields 2 variables, key and value). - The standard library provides
iter.Seq[V]anditer.Seq2[K, V]as convenient aliases for these signatures, but they are not strictly required for therangeloop to function. - The
for rangeloop body acts as an implicit closure passed to the iterator function, executing for each value yielded by the sequence.
- Go supports ranging over any function that matches specific iterator signatures:
Syntax Variations and Variable Omission
Go enforces strict variable usage; declared but unused variables cause compiler errors. The rules for omitting variables depend on whether the iterable yields one or two variables. Two-Variable Iterables (Arrays, Slices, Strings, Maps, 2-ary Functions): By default, these yield a key/index and a value.- Omitting the value: Drop the second variable entirely. The single variable will represent the key/index.
- Omitting the key: Use the blank identifier (
_) for the first variable.
- Standard usage: The single variable represents the value.
- Compiler Error: Attempting to use the blank identifier to “skip a key” (e.g.,
for _, value := range channel) will produce atoo many variables in rangecompiler error, as there is no second variable to assign.
Memory Semantics and Evaluation Rules
1. Value Copying The value variable in afor range loop is a copy of the element from the underlying data structure. Mutating the value variable does not modify the original iterable. To mutate the original structure, you must index into it using the key:
range is evaluated exactly once before the loop begins. This has critical implications depending on the underlying type:
- Arrays: Because the expression is evaluated once, ranging over an array copies the entire array. Mutations made to the original array during iteration are not seen by the loop.
- Array Pointers (
*[N]T): To avoid copying the entire array, you can range over a pointer to the array. This ensures that mutations to the array’s elements during iteration are visible to the loop. - Slices: Ranging over a slice only copies the slice header (which contains the pointer to the backing array, length, and capacity). Therefore, mutations to the underlying array’s elements during iteration are visible to the loop. However, appending to the slice will not increase the number of loop iterations, as the original length was evaluated at the start.
Master Go with Deep Grasping Methodology!Learn More





