Sobes.tech
Junior — Middle

What does the parameter before the method name in a function declaration indicate?

sobes.tech AI

Answer from AI

The parameter specified before the method name in a function declaration in Go is called a receiver. It links the function to a specific type, turning it into a method of that type.

A receiver can be a value or a pointer to a type and allows calling the method through an instance of that type.

Example:

type Point struct {
    X, Y int
}

// Method with a receiver of type Point
func (p Point) Move(dx, dy int) Point {
    p.X += dx
    p.Y += dy
    return p
}

// Method with a pointer receiver
func (p *Point) MoveInPlace(dx, dy int) {
    p.X += dx
    p.Y += dy
}

Here, (p Point) and (p *Point) are receivers that allow calling methods on objects of type Point.

What does the parameter before the method name in a… - sobes.tech