go-learning/tour_of_go/l5/methods.go
2025-08-31 17:38:50 +01:00

30 lines
484 B
Go

package main
import (
"fmt"
"math"
)
type Vertex struct {
X, Y float64
}
// This is a method - a function that implements a receiver (the part between "func" and "Abs()")
func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
// The method above has the same functionality as the function below
func Abs(v Vertex) float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func main() {
v := Vertex{3,4}
fmt.Println(v.Abs())
w := Vertex{3,4}
fmt.Println(Abs(w))
}