英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论