gpt4 book ai didi

去 "this"-关键字

转载 作者:IT老高 更新时间:2023-10-28 13:04:18 28 4
gpt4 key购买 nike

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

是否有任何理由反对使用 this 来引用当前对象,如下例所示?

type MyStruct struct {
someField string
}

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

最佳答案

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

它确实违反了 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.

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

关于去 "this"-关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29028512/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com