Source file tour/methods/methods-pointers-explained.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 Abs(v Vertex) float64 {
    15  	return math.Sqrt(v.X*v.X + v.Y*v.Y)
    16  }
    17  
    18  func Scale(v *Vertex, 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  	Scale(&v, 10)
    26  	fmt.Println(Abs(v))
    27  }
    28  

View as plain text