Go#5 Methods
http://go-tour-jp.appspot.com/#50
Goにはクラスという仕組みはないがstruct型にメソッドを定義する。
メソッドレシーバー (method receiver)
funcキーワードとメソッド名の間に、それ自身の引数リストを表現
下の例では、(v *Vertex)
という部分
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()) }
ここでの疑問
(v *Vertex)
の*
ってなんや?*
はポイント型を表す(参照?)- データがコピーデータではない(直接変更できる)