Skip to content

proposal: Go 2: add &range in for statements #21537

Closed
@pcostanza

Description

@pcostanza

When using the for statement to loop over a slice, it is sometimes useful or necessary to take the address of each slice entry rather than a value copy. However, doing this is a bit inconvenient and could be made more readable.

Loop over a slice by value:

for _, v := range s {
   ...
}

Loop over a slice by address:

for i := range s {
  p := &s[i]
  ...
}

Proposed syntactic extension to loop over a slice by address:

for _, p := &range s {
  ...
}

I believe the suggested extension is more readable and makes the intent clearer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions