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/14 08:44] – [Methods and type aliasing] rpjday | go_tour_notes [2018/04/14 08:57] (current) – [Refresher: pointer-based function] rpjday | ||
|---|---|---|---|
| Line 686: | Line 686: | ||
| ===== Methods with pointer receivers ===== | ===== Methods with pointer receivers ===== | ||
| - | ==== Refresher ==== | + | ==== Refresher: call-by-value |
| - | Simple example of call-by-value function | + | Call-by-value function, which makes a //copy// of the argument: |
| < | < | ||
| Line 711: | Line 711: | ||
| fmt.Println(v) | fmt.Println(v) | ||
| fmt.Println(Scale(v, | fmt.Println(Scale(v, | ||
| + | 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 | ||