方法接收器(Method receivers)

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

Method receivers

问题

Go方法接收器需要一个类型和一个类型的变量名,例如:

type MyFloat float64

func (x MyFloat) Abs() float64 {
    if x < 0 {
        return float64(-x)
    }
    return float64(x)
}

func main() {
    f := MyFloat(-math.Sqrt2)
    fmt.Println(f.Abs())
}

接收器在方法中使用x作为类型的变量名。为什么要指定x的名称呢?由于我可以在任何MyFloat的实例上调用该方法(不仅仅是命名为x的实例),为什么我还要指定x呢?既然接收器是一个类型或者类型的引用,为什么不像这样只接收类型或者指针:

func (MyFloat) Abs() float64 {
    if this < 0 {
        return float64(-this)
    }
    return float64(this)
}

我的假设是,与Java中的this不同,Go允许使用任何名称作为接收器的变量名。是这样吗?

英文:

Go method receivers take a type along with a variable name for the type, example:

type MyFloat float64

func (x MyFloat) Abs() float64 {
    if x &lt; 0 {
        return float64(-x)
    }
    return float64(x)
}

func main() {
    f := MyFloat(-math.Sqrt2)
    fmt.Println(f.Abs())
}

The receiver takes &quot;x&quot; along with the type receiving the method. What is the significance of the name 'x'. Since i am able to invoke the method on any instance of MyFloat ( not just on the one named as x ) why do i have to specify the x ? Since the receiver is a Type or a reference to a type why not simply take the type or the pointer alone like this

func (MyFloat) Abs() float64 {
    if this &lt; 0 {
        return float64(-this)
    }
    return float64(this)
}

My assumption is instead of this in Java golang allows any name? Is that so ?

答案1

得分: 3

你的假设是正确的:在方法定义中,接收者必须明确命名。这样可以避免任何歧义。在你的例子中,Go编译器怎么可能猜到x是接收者呢?

请注意,在Go语言中,使用"self"、"this"或"me"作为接收者名称被认为是不好的风格。接收者的名称应该简短,一个字母就足够了。更多信息请参考https://code.google.com/p/go-wiki/wiki/CodeReviewComments#Receiver_Names。

英文:

Your assumption is exact: the receiver has to be explicitly named in a method definition. It avoids any ambiguity. In your example, how could the Go compiler guess that x is the receiver?

Note that using "self" or "this" or "me" as the receiver name is considered as bad style in go. The name should be short - one letter is fine. See more information at https://code.google.com/p/go-wiki/wiki/CodeReviewComments#Receiver_Names

答案2

得分: 1

这是一个设计选择。

Java使用this,Go语言选择了另一种机制。

在Go中,将接收者设为指针或非指针都是合法的。

我们来看一下:

func (t Type)  Id()  { return t }
func (t *Type) IdPointer() { return t }

如果Go使用Java的设计会怎样呢?

它将变成:

func (Type)  Id()  { return this }
func (*Type) IdPointer() { return this }

首先,(*Type)是令人困惑的。

其次,this也可以是指针或值。同样令人困惑。

但是,无论如何,你可以像这样设计Go语言。

毕竟这是一个选择。

英文:

It's a design choice.

Java use this, Go-lang choose another mechanic.

In Go, it's legal to make the receiver a pointer or not.

Let's see:

func (t Type)  Id()  { return t }
func (t *Type) IdPointer() { return t }

What if Go use Java's design?

It will became:

func (Type)  Id()  { return this }
func (*Type) IdPointer() { return this }

Firstly, it is confused that what (*Type) is.

Secondly, this can also be a pointer or a value. Also confused.

But, anyway, you can design Go-lang like this.

It is a choice after all.

答案3

得分: 0

我认为你没有正确使用,你应该在结构体中使用它。接收器应该引用结构体的字段。

例如:

package main
import "fmt"

type Decimal struct {
    first float64
}

func (x Decimal) out() float64 {
    return x.first
}

func main() {
    var start Decimal
    start.first = 10.8
    show := start.out()
    fmt.Println(show)
}

请注意,这是一个示例代码,用于演示如何在结构体中使用方法。

英文:

I think you are not using correctly you should use it in a struct.Where the receiver makes a reference to the struct's fields.

For example:

package main
import &quot;fmt&quot;

type Decimal struct {
    first float64
}

func (x Decimal) out() float64 {
    return x.first
}
func main() {
    var start Decimal
    start.first = 10.8
    show := start.out()
    fmt.Println(show)
}

huangapple
  • 本文由 发表于 2014年8月17日 04:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/25343853.html
匿名

发表评论

匿名网友

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

确定