Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| go_tour_notes [2018/04/13 09:13] – [Methods and type aliasing] rpjday | go_tour_notes [2018/04/14 08:57] (current) – [Refresher: pointer-based function] rpjday | ||
|---|---|---|---|
| Line 623: | Line 623: | ||
| ===== Methods ===== | ===== Methods ===== | ||
| - | ==== Non-method Abs() ==== | + | ==== Non-method Abs() function |
| Non-method '' | Non-method '' | ||
| Line 651: | Line 651: | ||
| } | } | ||
| - | func (v Vertex) Abs() float64 { | + | func (v Vertex) Abs() float64 { // receiver argument |
| return math.Sqrt(v.X*v.X + v.Y*v.Y) | return math.Sqrt(v.X*v.X + v.Y*v.Y) | ||
| } | } | ||
| Line 662: | Line 662: | ||
| </ | </ | ||
| - | ==== Methods and type aliasing ==== | + | ==== Methods and type "aliasing" |
| You can declare a method on non-struct types, too. Below, we see a numeric type '' | You can declare a method on non-struct types, too. Below, we see a numeric type '' | ||
| Line 684: | Line 684: | ||
| You can only declare a method with a receiver whose type is defined in the same package as the method. You //cannot// declare a method with a receiver whose type is defined in another package (which includes the built-in types such as '' | You can only declare a method with a receiver whose type is defined in the same package as the method. You //cannot// declare a method with a receiver whose type is defined in another package (which includes the built-in types such as '' | ||
| - | ==== Pointer | + | ===== Methods with pointer |
| + | |||
| + | ==== Refresher: call-by-value ==== | ||
| + | |||
| + | Call-by-value function, which makes a //copy// of the argument: | ||
| < | < | ||
| + | package main | ||
| + | |||
| + | import ( | ||
| + | " | ||
| + | ) | ||
| + | |||
| type Vertex struct { | type Vertex struct { | ||
| - | X, Y float64 | + | |
| } | } | ||
| - | func (v Vertex) | + | func Scale(v Vertex, f float64) Vertex |
| - | return math.Sqrt(v.X*v.X + v.Y*v.Y) | + | v.X = v.X * f |
| + | | ||
| + | return | ||
| } | } | ||
| - | func (v *Vertex) Scale(f float64) { | + | func main() { |
| - | v.X = v.X * f | + | v := Vertex{3, 4} |
| - | v.Y = v.Y * f | + | |
| + | fmt.Println(Scale(v, 10)) | ||
| + | fmt.Println(v) | ||
| } | } | ||
| + | |||
| + | {3 4} | ||
| + | {30 40} | ||
| + | {3 4} | ||
| </ | </ | ||
| + | |||
| + | ==== Refresher: pointer-based function ==== | ||
| + | |||
| + | Pointer-based call: | ||
| + | |||
| + | < | ||
| + | package main | ||
| + | |||
| + | import ( | ||
| + | " | ||
| + | ) | ||
| + | |||
| + | type Vertex struct { | ||
| + | X, Y float64 | ||
| + | } | ||
| + | |||
| + | func Scale(v *Vertex, f float64) { | ||
| + | v.X = v.X * f | ||
| + | v.Y = v.Y * f | ||
| + | } | ||
| + | |||
| + | func main() { | ||
| + | v := Vertex{3, 4} | ||
| + | fmt.Println(v) | ||
| + | Scale(& | ||
| + | fmt.Println(v) | ||
| + | } | ||
| + | |||
| + | {3 4} | ||
| + | {30 40} | ||
| + | </ | ||
| + | |||
| + | Notes about the above: | ||
| + | |||
| + | * Even if the arg is a pointer, Go allows you to use " | ||
| + | * You cannot support both variations of that function | ||