Go "this"-keyword

huangapple go评论76阅读模式
英文:

Go "this"-keyword

问题

在深入研究文档后,我找不到对我以下问题的答案:

在下面的示例中,使用this来引用当前对象是否有任何理由?

type MyStruct struct {
  someField string
}

func (this MyStruct) getSomeField() string {
  return this.someField
}
英文:

After diving into the docs I couldn't find the answer to my following question:

Is there any reason against using this for referring to the current object as in the following example?

type MyStruct struct {
  someField string
}

func (this MyStruct) getSomeField() string {
  return this.someField
}

答案1

得分: 25

没有技术上的理由不这样做。

这确实违反了通用指南,如这里所解释的:

> 不要使用诸如“me”、“this”或“self”之类的通用名称,这些标识符是面向对象语言的典型特征,更强调方法而不是函数。

我还想补充一点,在使用this(或self)的语言中,this始终是一个指针。对于Go中的方法接收者,情况并非总是如此。

英文:

There is no technical reason not to do this.

It does go against the general guidelines as explained here:

> Don't use generic names such as "me", "this" or "self", identifiers typical of object-oriented languages that place more emphasis on methods as opposed to functions.

I would also like to add that in languages that use this (or self), this is always a pointer. For method receivers in Go, this is not necessarily the case.

huangapple
  • 本文由 发表于 2015年3月13日 17:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/29028512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定