I'm getting the linting error
ineffective assignment to field Player.Level (SA4005)go-staticcheck
when I try to use a struct method LevelUp
to update the struct's value Player.Level
:
func main() {
player := Player{
Name: "Tom",
Level: 0,
}
player.LevelUp()
fmt.Printf("Player level %d\n", player.Level)
}
type Player struct {
Name string
Level int
}
func (p Player) LevelUp() {
p.Level += 1 // linting error here
}
p.Level
also remains 0
after calling p.LevelUp()
. What is the proper way to call a method that updates the value of a field of the struct this method is attached to?
Output:
Player level 0