Solution Review: Coordinates of a Point
This lesson discusses the solution to the challenge given in the previous lesson.
package mainimport ("fmt""math")type Point struct { /// struct of type PointX, Y float64}func (p *Point)Abs() float64 { // method calculating absolute valuereturn math.Sqrt(float64(p.X*p.X + p.Y*p.Y))}func (p *Point)Scale(s float64) { // method to scale a pointp.X = p.X * sp.Y = p.Y * sreturn}func main() {p1 := new(Point)p1.X = 3p1.Y = 4fmt.Printf("The length of the vector p1 is: %f\n", p1.Abs() ) // calling Abs() funcp2:= &Point{4, 5}fmt.Printf("The length of the vector p2 is: %f\n", p2.Abs() ) // calling Abs() funcp1.Scale(5) // calling Scale() fucnfmt.Printf("The length of the vector p1 is: %f\n", p1.Abs() ) // calling Abs() funcfmt.Printf("Point p1 scaled by 5 has the following coordinates: X %f - Y %f", p1.X, p1.Y)}
Get hands-on with 1400+ tech skills courses.