diff --git a/indirection-values.go b/indirection-values.go new file mode 100644 index 0000000..4b5a620 --- /dev/null +++ b/indirection-values.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +func (v Vertex) Abs() float64 { + return math.Sqrt(v.X * v.X + v.Y * v.Y) +} + +func AbsFunc(v Vertex) float64 { + return math.Sqrt(v.X * v.X + v.Y * v.Y) +} + +func main() { + v := Vertex{3, 4} + fmt.Println(v.Abs()) + fmt.Println(AbsFunc(v)) + + p := &Vertex{4, 3} + fmt.Println(p.Abs()) + fmt.Println(AbsFunc(*p)) +} diff --git a/indirection.go b/indirection.go new file mode 100644 index 0000000..f7e78df --- /dev/null +++ b/indirection.go @@ -0,0 +1,29 @@ +package main + +import "fmt" + +type Vertex struct { + X, Y float64 +} + +func (v *Vertex) Scale(f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func ScaleFunc(v *Vertex, f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func main() { + v := Vertex{3, 4} + v.Scale(2) + ScaleFunc(&v, 10) + + p := &Vertex{4, 3} + p.Scale(3) + ScaleFunc(p, 8) + + fmt.Println(v, p) +} diff --git a/interfaces.go b/interfaces.go new file mode 100644 index 0000000..bc71b13 --- /dev/null +++ b/interfaces.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "math" +) + +type Abser interface { + Abs() float64 +} + +func main() { + var a Abser + f := MyFloat(-math.Sqrt2) + v := Vertex{3, 4} + + a = f // a MyFloat implements Abser + a = &v // a *Vertex impelments Abser + + // In the following line, v is a Vertex (not *Vertex) + // and does NOT implement Abser. + // a = v + + fmt.Println(a.Abs()) +} + +type MyFloat float64 + +func (f MyFloat) Abs() float64 { + if f < 0 { + return float64(-f) + } + return float64(f) +} + +type Vertex struct { + X, Y float64 +} + +func (v *Vertex) Abs() float64 { + return math.Sqrt(v.X * v.X + v.Y * v.Y) +} diff --git a/methods-continued.go b/methods-continued.go new file mode 100644 index 0000000..4863131 --- /dev/null +++ b/methods-continued.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "math" +) + +type MyFloat float64 + +func (f MyFloat) Abs() float64 { + if f < 0 { + return float64(-f) + } + return float64(f) +} + +func main() { + f := MyFloat(-math.Sqrt2) + fmt.Println(f.Abs()) +} diff --git a/methods-funcs.go b/methods-funcs.go new file mode 100644 index 0000000..b4f2e30 --- /dev/null +++ b/methods-funcs.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +func Abs(v Vertex) float64 { + return math.Sqrt(v.X * v.X + v.Y * v.Y) +} + +func main() { + v := Vertex{3 ,4} + fmt.Println(Abs(v)) +} diff --git a/methods-pointers-explained.go b/methods-pointers-explained.go new file mode 100644 index 0000000..372ba92 --- /dev/null +++ b/methods-pointers-explained.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +func Abs(v Vertex) float64 { + return math.Sqrt(v.X * v.X + v.Y * v.Y) +} + +func Scale(v *Vertex, f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func main() { + v := Vertex{3, 4} + Scale(&v, 10) + fmt.Println(Abs(v)) +} diff --git a/methods-pointers.go b/methods-pointers.go new file mode 100644 index 0000000..dd74b94 --- /dev/null +++ b/methods-pointers.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +func (v Vertex) Abs() float64 { + return math.Sqrt(v.X * v.X + v.Y * v.Y) +} + +func (v *Vertex) Scale(f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func main() { + v := Vertex{3, 4} + v.Scale(10) + fmt.Println(v.Abs()) +} diff --git a/methods-with-pointer-receivers.go b/methods-with-pointer-receivers.go new file mode 100644 index 0000000..9c1524c --- /dev/null +++ b/methods-with-pointer-receivers.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +func (v *Vertex) Scale(f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func (v *Vertex) Abs() float64 { + return math.Sqrt(v.X * v.X + v.Y * v.Y) +} + +func main() { + v := &Vertex{3, 4} + fmt.Printf("Before scaling: %+v, Abs: %v\n", v, v.Abs()) + v.Scale(5) + fmt.Printf("After scaling: %+v, Abs: %v\n", v, v.Abs()) +} diff --git a/methods.go b/methods.go new file mode 100644 index 0000000..fe7f9ab --- /dev/null +++ b/methods.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +func (v Vertex) Abs() float64 { + return math.Sqrt(v.X * v.X + v.Y * v.Y) +} + +func main() { + v := Vertex{3 ,4} + fmt.Println(v.Abs()) +}