去“这个” - 关键字 [英] Go "this"-keyword

查看:187
本文介绍了去“这个” - 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否有任何理由反对使用这个

code>用于引用当前对象,如下例所示:

  type MyStruct struct {
someField字符串
}

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


解决方案

没有技术性的理由不这样做。 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.

这篇关于去“这个” - 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆