Skip to content

Item #32 Ignoring the impact of using pointer elements in range loops - not relevant after Go 1.22Β #128

@iypetrov

Description

@iypetrov

Starting with Go 1.22, iteration variables declared by range loops are created anew for each iteration, instead of being reused across iterations. Because of this the first example with the Store and Client structs doesn't return

key=1, value=&main.Customer{ID:"3", Balance:0}
key=2, value=&main.Customer{ID:"3", Balance:0}
key=3, value=&main.Customer{ID:"3", Balance:0}

but

key=1, value=&main.Customer{ID:"1", Balance:10}
key=2, value=&main.Customer{ID:"2", Balance:-10}
key=3, value=&main.Customer{ID:"3", Balance:0}

https://go.dev/doc/go1.22#language

I think that a small comment on the code example is enough to warn the readers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    community mistakeMistakes proposed by the community

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions