Last active
August 29, 2015 14:13
-
-
Save sugilog/edf8ec18c123a3c87a8b to your computer and use it in GitHub Desktop.
構造体へのメソッド定義
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
type Vertex struct { | |
X, Y MyFloat | |
} | |
type MyFloat float64 | |
func ( v *Vertex ) Abs() float64 { | |
return math.Sqrt( float64( v.X.Sq() + v.Y.Sq() ) ) | |
} | |
func (v Vertex) AnotherAbs() float64 { | |
return math.Sqrt( float64( v.X*v.X + v.Y*v.Y ) ) | |
} | |
func (ver *Vertex) Add(i MyFloat) { | |
ver.X += i | |
ver.Y += 1 | |
} | |
func ( ver Vertex ) FakeAdd( i MyFloat ) { | |
ver.X += i | |
ver.Y += 1 | |
} | |
func ( f MyFloat ) Sq() MyFloat { | |
return f * f | |
} | |
func main() { | |
v := &Vertex{ 3, 4 } | |
fmt.Println( v.Abs() ) | |
v.Add( 2 ) | |
fmt.Println( v.Abs() ) | |
v.FakeAdd( 3 ) | |
fmt.Println( v.Abs() ) | |
fmt.Println( v.AnotherAbs() ) | |
v.Add( 3 ) | |
fmt.Println( v.Abs() ) | |
fmt.Println( v.AnotherAbs() ) | |
} | |
//=> | |
// 5 | |
// 7.0710678118654755 | |
// 7.0710678118654755 | |
// 7.0710678118654755 | |
// 10 | |
// 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment