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/13 08:35] – [Basic methods] rpjdaygo_tour_notes [2018/04/14 08:57] (current) – [Refresher: pointer-based function] rpjday
Line 1: Line 1:
 ===== STUFF (tour) ===== ===== STUFF (tour) =====
  
-  * explain early that functions are not polymorphic +  * explain early that functions are not polymorphic (basics section) 
-  * earlier: type aliasing+  * earlier: type aliasing (which introduces a new type, not just an alias, except for byte and rune)
   * swap first two pages of Methods section   * swap first two pages of Methods section
 +  * I would move closures later (possibly after methods)
  
 ===== Welcome ===== ===== Welcome =====
Line 622: Line 623:
 ===== Methods ===== ===== Methods =====
  
-==== Non-method ====+==== Non-method Abs() function ====
  
 Non-method ''Abs()'' function for ''Vertex'', which takes a ''Vertex'' as a regular argument (no polymorphism) Non-method ''Abs()'' function for ''Vertex'', which takes a ''Vertex'' as a regular argument (no polymorphism)
Line 650: 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 661: Line 662:
 </code> </code>
  
-==== Methods and type aliasing ====+==== Methods and type "aliasing====
  
 You can declare a method on non-struct types, too. Below, we see a numeric type ''MyFloat'' with an ''Abs'' method: You can declare a method on non-struct types, too. Below, we see a numeric type ''MyFloat'' with an ''Abs'' method:
Line 682: Line 683:
  
 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 ''int''). 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 ''int'').
 +
 +===== Methods with pointer receivers =====
 +
 +==== Refresher: call-by-value ====
 +
 +Call-by-value function, which makes a //copy// of the argument:
 +
 +<code>
 +package main
 +
 +import (
 +        "fmt"
 +)
 +
 +type Vertex struct {
 +        X, Y float64
 +}
 +
 +func Scale(v Vertex, f float64) Vertex {
 +        v.X = v.X * f
 +        v.Y = v.Y * f
 +        return v
 +}
 +
 +func main() {
 +        v := Vertex{3, 4}
 +        fmt.Println(v)
 +        fmt.Println(Scale(v, 10))
 +        fmt.Println(v)
 +}
 +
 +{3 4}
 +{30 40}
 +{3 4}
 +</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.1523608553.txt.gz
  • Last modified: 2018/04/13 08:35
  • by rpjday