Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Sets (Pointer vs Value Receiver)

Tags:

I am having a hard time understanding as to why are these rules associated with method set of pointer type .vs. value type

Can someone please explain the reason (from the interface table perspective)

(Snippet from William Kennedy's blog)

Values          Methods Receivers ----------------------------------------------- T               (t T) *T              (t T) and (t *T)  Methods Receivers    Values ----------------------------------------------- (t T)                 T and *T (t *T)                *T 

Snippet from specification

Method sets

A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T). Further rules apply to structs containing anonymous fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.

The method set of a type determines the interfaces that the type implements and the methods that can be called using a receiver of that type.

like image 796
user2780187 Avatar asked Nov 07 '15 20:11

user2780187


People also ask

What is the difference between a pointer receiver and a value receiver?

Methods with pointer receivers can modify the value to which the receiver points (as scale does here). Since methods often need to modify their receiver, pointer receivers are more common than value receivers. With a value receiver, the scale method operates on a copy of the original Point value.

Is it possible to call methods that are declared with value receiver using a pointer?

you can mix and match methods with value receivers and methods with pointer receivers, and use them with variables containing values and pointers, without worrying about which is which. Both will work, and the syntax is the same.

When would you use a pointer receiver?

There are two reasons to use a pointer receiver. The first is so that the method can modify the value that its receiver points to. The second is to avoid copying the value on each method call. This can be more efficient if the receiver is a large struct, for example.

What is pointer receiver go?

You can declare methods with pointer receivers. This means the receiver type has the literal syntax *T for some type T . (Also, T cannot itself be a pointer such as *int .) For example, the Scale method here is defined on *Vertex .


2 Answers

  1. If you have a *T you can call methods that have a receiver type of *T as well as methods that have a receiver type of T (the passage you quoted, Method Sets).
  2. If you have a T and it is addressable you can call methods that have a receiver type of *T as well as methods that have a receiver type of T, because the method call t.Meth() will be equivalent to (&t).Meth() (Calls).
  3. If you have a T and it isn't addressable (for instance, the result of a function call, or the result of indexing into a map), Go can't get a pointer to it, so you can only call methods that have a receiver type of T, not *T.
  4. If you have an interface I, and some or all of the methods in I's method set are provided by methods with a receiver of *T (with the remainder being provided by methods with a receiver of T), then *T satisfies the interface I, but T doesn't. That is because *T's method set includes T's, but not the other way around (back to the first point again).

In short, you can mix and match methods with value receivers and methods with pointer receivers, and use them with variables containing values and pointers, without worrying about which is which. Both will work, and the syntax is the same. However, if methods with pointer receivers are needed to satisfy an interface, then only a pointer will be assignable to the interface — a value won't be valid.

like image 58
hobbs Avatar answered Oct 21 '22 21:10

hobbs


From Golang FAQ:

As the Go specification says, the method set of a type T consists of all methods with receiver type T, while that of the corresponding pointer type *T consists of all methods with receiver *T or T. That means the method set of *T includes that of T, but not the reverse.

This distinction arises because if an interface value contains a pointer *T, a method call can obtain a value by dereferencing the pointer, but if an interface value contains a value T, there is no safe way for a method call to obtain a pointer. (Doing so would allow a method to modify the contents of the value inside the interface, which is not permitted by the language specification.)

Even in cases where the compiler could take the address of a value to pass to the method, if the method modifies the value the changes will be lost in the caller. As an example, if the Write method of bytes.Buffer used a value receiver rather than a pointer, this code:

var buf bytes.Buffer io.Copy(buf, os.Stdin) 

would copy standard input into a copy of buf, not into buf itself. This is almost never the desired behavior.

About Golang interface under the hood.

  • Go interface by Lance Taylor
  • Go interface by Russ Cox
like image 21
Dan Super Avatar answered Oct 21 '22 21:10

Dan Super