fghf
fghf

Reputation: 734

How to change value in go array?

Here is what I am trying todo

for _,p := range *players {
        for _,tp := range *tournamentPlayers{
            if p.Id==tp.PlayerId {
                p.Points += tp.Prize

            }
        }
    }

after for nothing is saved

Upvotes: 0

Views: 262

Answers (1)

nouney
nouney

Reputation: 4411

When you range over an array, the second variable will be a copy of the value. So when you're modifying it, you don't actually modify the value stored in the array.

You need to use the index:

for i := range *players {
        for _,tp := range *tournamentPlayers{
            if players[i].Id==tp.PlayerId {
                players[i].Points += tp.Prize

            }
        }
    }

You'll find more informations in the spec.

Upvotes: 4

Related Questions