added file to play with methods

This commit is contained in:
Stephen M. McQuay
2012-08-27 22:28:15 -06:00
parent c94a5c6613
commit 64b1c50ed9
2 changed files with 33 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
package main
import (
"fmt"
)
type vertex struct {
x, y float64
}
// note that this one can deal fine with a copy:
func (v vertex) abs() float64 {
return v.x + v.y
}
// but this function needs access to the actual struct members
func (v *vertex) scale(s float64) {
v.x *= s
v.y *= s
}
func main() {
v := vertex{1.1, 2.2}
fmt.Println(v.abs())
v.scale(12)
fmt.Println(v.abs())
}