英文:
Ineffective Assignment to Field when trying to update a Struct in Go
问题
我遇到了一个代码错误,当我尝试使用结构体方法LevelUp
来更新结构体的值Player.Level
时,出现了以下的linting错误:
ineffective assignment to field Player.Level (SA4005)go-staticcheck
在调用p.LevelUp()
之后,p.Level
仍然保持为0
。请问如何正确调用一个方法来更新结构体中的字段值?
英文:
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
答案1
得分: 16
每个参数,包括接收器,在进入函数/方法时都会被复制。当你返回时,对副本所做的更改将丢失。这就是为什么你会收到一个警告:你修改了一个你从未使用过的字段:你在赋值后的方法中没有使用它,而且你不可能在其他任何地方使用它,因为从方法返回后,赋值的效果就会丢失。
如果你需要保留更改,你必须使用指针接收器(p *Player
)并修改指向的对象(p.Level++
就会做到这一点)。
func (p *Player) LevelUp() {
p.Level++
}
这将输出(在Go Playground上尝试一下):
Player level 1
参考链接:
英文:
Each parameter including the receiver is copied upon entering the function / method. When you return, the changes made to the copy are lost. That's why you get a warning: you modify a field which you never use: you don't use in in the method after the assignment, and you can't possibly use it anywhere else, because after returning from the method, the effect of the assignment is lost.
If you need to retain the changes, you must use a pointer receiver (p *Player
) and modify the pointed object (p.Level++
will do just that).
func (p *Player) LevelUp() {
p.Level++
}
This will output (try it on the Go Playground):
Player level 1
See related:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论