30 lines
484 B
Go
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))
|
|
}
|