commit more tour exercises
This commit is contained in:
parent
3b30db090b
commit
ce06a799d1
28
indirection-values.go
Normal file
28
indirection-values.go
Normal file
@ -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))
|
||||
}
|
29
indirection.go
Normal file
29
indirection.go
Normal file
@ -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)
|
||||
}
|
42
interfaces.go
Normal file
42
interfaces.go
Normal file
@ -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)
|
||||
}
|
20
methods-continued.go
Normal file
20
methods-continued.go
Normal file
@ -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())
|
||||
}
|
19
methods-funcs.go
Normal file
19
methods-funcs.go
Normal file
@ -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))
|
||||
}
|
25
methods-pointers-explained.go
Normal file
25
methods-pointers-explained.go
Normal file
@ -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))
|
||||
}
|
25
methods-pointers.go
Normal file
25
methods-pointers.go
Normal file
@ -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())
|
||||
}
|
26
methods-with-pointer-receivers.go
Normal file
26
methods-with-pointer-receivers.go
Normal file
@ -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())
|
||||
}
|
19
methods.go
Normal file
19
methods.go
Normal file
@ -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())
|
||||
}
|
Loading…
Reference in New Issue
Block a user