Skip to content

range-value-address

Avoid taking addresses of range values.

Updated View as Markdown

Default severity: warning

Taking the address of a range value points to the iteration copy, not the corresponding slice or array element. Go 1.22 made variables declared with := iteration-local, so this is no longer the classic shared-pointer bug, but the pointer still does not refer back to the source collection. Use an index when that source identity is intended.

Bad

for _, value := range values {
	pointers = append(pointers, &value)
}

Good

for index := range values {
	pointers = append(pointers, &values[index])
}
Navigation

Type to search…

↑↓ navigate↵ selectEsc close