Skip to content

Instantly share code, notes, and snippets.

@sugilog
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugilog/edf8ec18c123a3c87a8b to your computer and use it in GitHub Desktop.
Save sugilog/edf8ec18c123a3c87a8b to your computer and use it in GitHub Desktop.
構造体へのメソッド定義
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