Functions_Closures Language

Methods with Struct

type Point struct {
     x  float64
     y  float64

The Point struct type has two fields, x and y of float64

func (p Point) DistToOrgin()  {
       t := math.Pow(p.x, 2) + math.Pow(p.y, 2)​
       return math.Sqrt(t)​

A method is defined with use of the keyword “func” just like a definition of the function. Just after func keyword, the defined fields of struct used in the body of code of the function as a receiver. It takes a variable of struct type “p Point” as a receiver (input) to the method name DistToOrgin().