Parameter between 'func' and the function name

In Go language, there can be an optional set of parentheses between func and the function name, which is used to specify the function's receiver type.

Here's an example of a function signature with a receiver type:

type Point struct {
    X, Y float64
}

func (p Point) Distance() float64 {
    return math.Sqrt(p.X*p.X + p.Y*p.Y)
}

In this example, the function name is Distance, and it has a receiver of type Point specified in parentheses between func and the function name. This means that the Distance function is a method of the Point type.

The receiver type is used to specify which struct or type the method belongs to, and it is accessed using the . operator on a value of that type. For example, if p is a variable of type Point, we can call the Distance method like this:

p := Point{X: 3, Y: 4}
fmt.Println(p.Distance())

This will output 5, which is the distance of the Point (3, 4) from the origin.

I hope this clears up any confusion.

Last updated