Go methods for types

You can define methods on struct types.

The method receiver appears in its own argument list between the func keyword and the method name.

The code snipers here just run as it looks like, so no results given. 

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 { //一个Vertex指针类型的接收器,也是方法调用者
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := &Vertex{3, 4}
    fmt.Println(v.Abs())
}

Actually, you can define a method on any type you define in your package, not just structs.

NOTE: You cannot define a method on a type from another package, or on a basic type.

package main

import (
    "fmt"
    "math"
)

type MyFloat float64

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

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

This is a method for float64 type, note that we do not use a pointer as a receiver.

So the main difference: first, pointer will avoid copying the value on each method call (more efficient if the value type is a large struct). Second, so that the method can modify the value that the pointer receiver points to.

区别在于:指针接收器能避免每次方法调用时进行类型的拷贝(比如一个庞大的结构体类型),另外指针接收器可以修改它所指向的具体类型实例的值。

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Scale(f float64) {
    v.X = v.X * f
    v.Y = v.Y * f
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := &Vertex{3, 4}
    v.Scale(5)
    fmt.Println(v, v.Abs()) //如果全换成v Vertex的话,Scale操作将不会影响v.Abs()的结果。
}


更有甚者,接口的实现也要靠method for types!

package main

import (
    "fmt"
    "math"
)

type Abser interface {
    Abs() float64
}

type MyFloat float64

type Vertex struct {
    X, Y float64
}

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

func (v *Vertex) Abs() float64 { //具体的Abs()方法实现,同时指定了调用者的类型(*Vertex)
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    var a Abser
    f := MyFloat(-math.Sqrt2)
    v := Vertex{3, 4}

    a = f  // a MyFloat 实现了 Abser
    a = &v // a *Vertex 实现了 Abser

    // 下面一行,v 是一个 Vertex(而不是 *Vertex)
    // 所以没有实现 Abser。
    // a = v

    fmt.Println(a.Abs())
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值