go-learning/tour_of_go/l5/methods.go

30 lines
484 B
Go
Raw Permalink Normal View History

2025-08-31 16:38:50 +00:00
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))
}