Source file tour/methods/methods-with-pointer-receivers.go

     1  //go:build OMIT
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"math"
     8  )
     9  
    10  type Vertex struct {
    11  	X, Y float64
    12  }
    13  
    14  func (v *Vertex) Scale(f float64) {
    15  	v.X = v.X * f
    16  	v.Y = v.Y * f
    17  }
    18  
    19  func (v *Vertex) Abs() float64 {
    20  	return math.Sqrt(v.X*v.X + v.Y*v.Y)
    21  }
    22  
    23  func main() {
    24  	v := &Vertex{3, 4}
    25  	fmt.Printf("Before scaling: %+v, Abs: %v\n", v, v.Abs())
    26  	v.Scale(5)
    27  	fmt.Printf("After scaling: %+v, Abs: %v\n", v, v.Abs())
    28  }
    29  

View as plain text