JTW
JTW

Reputation: 3685

What is the difference between a method receiver and parameter?

Looking at the Go documentation shown below, I'm having trouble understanding the distinction between receivers and parameters:

 func (p *Page) save() error {
     filename := p.Title + ".txt"
     return ioutil.WriteFile(filename, p.Body, 0600)
 }

This method's signature reads: This is a method named save that takes as its receiver p, a pointer to Page . It takes no parameters, and returns a value of type error.

Upvotes: 6

Views: 3135

Answers (1)

user1804599
user1804599

Reputation:

The receiver is like this in C#: in x.f(a, b, c) the receiver is x and the arguments are a, b and c. When the function is executed the parameters refer to copies of the arguments. The important difference between the receiver and parameters is that when the receiver is an interface type at the call site, the function to be called is determined dynamically rather than statically.

Upvotes: 11

Related Questions