且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

去“这个” - 关键字

更新时间:2023-11-10 18:07:04

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

它与此处所述的一般准则不符:


不要使用通用名称,例如me,this或self而不是功能。

我还想在使用这个(或 self ),这个总是一个指针。对于Go中的方法接收器,情况并非一定如此。

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
}

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.