Why are receivers pass by value in Go?

后端 未结 3 1118
野趣味
野趣味 2020-12-30 08:02

Seems like you\'d ALWAYS want this:

func (self *Widget) Do() {
}

instead of this

func (self Widget) Do() {
}
3条回答
  •  盖世英雄少女心
    2020-12-30 08:19

    Seems like you'd ALWAYS want this:

    No. The value receiver is more general. It can be used in all the places that a pointer receiver can; but a pointer receiver cannot be used in all the places that a value receiver can -- for example, if you have an rvalue expression of the type Widget; you can call value-receiver methods on it, but not pointer-receiver methods.

提交回复
热议问题