Source file tour/methods/methods-pointers.go

     1  // +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) Abs() float64 {
    15  	return math.Sqrt(v.X*v.X + v.Y*v.Y)
    16  }
    17  
    18  func (v *Vertex) Scale(f float64) {
    19  	v.X = v.X * f
    20  	v.Y = v.Y * f
    21  }
    22  
    23  func main() {
    24  	v := Vertex{3, 4}
    25  	v.Scale(10)
    26  	fmt.Println(v.Abs())
    27  }
    28  

View as plain text