Change the actual value using the reference
- related
- Learn go with tests
Take a look at this piece of code
func (f *FileSystemPlayerStore) RecordWin(name string) {
league := f.GetLeague()
for i, player := range league {
if player.Name == name {
league[i].Wins++
}
}
f.database.Seek(0,0)
json.NewEncoder(f.database).Encode(league)
}
You may be asking yourself why I am doing league[i].Wins++
rather than player.Wins++
When you range over a slice you are returned the current index of the loop (in our case i
) and a copy of the element at that index
We need to get the reference to the actual value by doing league[i]
and then changing the value instead.