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()) }
有疑问加站长微信联系(非本文作者)