Methods
Example (Method)
package main
import (
"fmt"
"math"
)
type Vertex struct {
X, Y float64
}
func (v Vertex) Dist() float64 {
return math.Sqrt(v.X * v.X + v.Y * v.Y)
}
func main() {
v := Vertex{5, 12}
fmt.Println(v.Dist())
}Example (Regular Function)
Methods on Non-Struct Types
Pointer Receivers
Example 1 (Pointer Receiver)
Example 2 (Value Receiver)
Example 3 (Function Version)
Methods and Pointer Indirection 1
Example 4
Methods and Pointer Indirection 2
Example
Choosing a Value or Pointer Receiver
Last updated