Source file
tour/methods/indirection-values.go
1
2
3 package main
4
5 import (
6 "fmt"
7 "math"
8 )
9
10 type Vertex struct {
11 X, Y float64
12 }
13
14 func (v Vertex) Abs() float64 {
15 return math.Sqrt(v.X*v.X + v.Y*v.Y)
16 }
17
18 func AbsFunc(v Vertex) float64 {
19 return math.Sqrt(v.X*v.X + v.Y*v.Y)
20 }
21
22 func main() {
23 v := Vertex{3, 4}
24 fmt.Println(v.Abs())
25 fmt.Println(AbsFunc(v))
26
27 p := &Vertex{4, 3}
28 fmt.Println(p.Abs())
29 fmt.Println(AbsFunc(*p))
30 }
31
View as plain text