Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
go_tour_notes [2018/04/14 08:45] – [Refresher] rpjdaygo_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 and returning the result:+Call-by-value function, which makes a //copy// of the argument:
  
 <code> <code>
Line 719: Line 719:
 </code> </code>
  
 +==== Refresher: pointer-based function ====
  
 +Pointer-based call:
 +
 +<code>
 +package main
 +
 +import (
 +        "fmt"
 +)
 +
 +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(&v, 10)
 +        fmt.Println(v)
 +}
 +
 +{3 4}
 +{30 40}
 +</code>
 +
 +Notes about the above:
 +
 +  * Even if the arg is a pointer, Go allows you to use "." to dereference.
 +  * You cannot support both variations of that function
  • go_tour_notes.1523695536.txt.gz
  • Last modified: 2018/04/14 08:45
  • by rpjday