go this(またはselfPython)の構成とは何ですか?
type Shape struct {
isAlive bool
}
func (shape *Shape) setAlive(isAlive bool) {
}
関数でsetAliveどのようにすればよいthis.isAlive = isAlive;ですか?
Go のメソッド宣言には、メソッド名の前にいわゆるレシーバーがあります。あなたの例ではそれは(shape *Shape)です。呼び出すfoo.setAlive(false) fooと に渡されshapeますsetAlive。
したがって、基本的に以下は構文糖衣です
func (shape *Shape) setAlive(isAlive bool) {
shape.isAlive = isAlive
}
foo.setAlive(false)
為に
func setAlive(shape *Shape, isAlive bool) {
shape.isAlive = isAlive
}
setAlive(foo, false)
あなたの例でshapeは、レシーバーです。次のように書くこともできます。
func (this *Shape) setAlive(isAlive bool) {
this.isAlive = isAlive
}