golang-pitfalls-control-structures
Installation
SKILL.md
Golang Pitfalls: Control Structures
Source material: mistakes #30-35 from 100 Go Mistakes and How to Avoid Them (teivah/100-go-mistakes).
Apply these rules when writing Go loops and control flow.
30. Elements are copied in range loops (#30)
- The
valueelement of arangeloop is a copy. Mutating it does not mutate the collection element (unless the element/field is a pointer). - To mutate structs in a slice: access via index
s[i](range or classicfor).
for i := range customers {
customers[i].age = age // works
}
for _, c := range customers {
c.age = age // no-op on the actual element
}