liveBook · Manning

Value receiver

A value receiver is a method receiver declared as a non-pointer value, causing the method to operate on a copy of the receiver.

How 3 Manning books explain it

100 Go Mistakes and How to Avoid Them

A value receiver is a Go method receiver declared as a value, so the method operates on a copy of the receiver value.

How it is used
Guides method receiver choice when copying affects mutation, deferred calls, or synchronization.
How deep it goes
Traces observable effects of receiver copies through mutation, defer, and mutex use, while leaving detailed pointer-versus-value performance analysis aside.
Examples
Contrasts balance updates, deferred printing of `foo` versus `bar`, and a concurrent counter whose copied mutex fails to protect updates.
What you can do after
Diagnose/debug with it when a method fails to mutate its original receiver or copying a mutex causes a race.

Read how 100 Go Mistakes and How to Avoid Them explains value receiver →

Learn Go with Pocket-Sized Projects

A value receiver is a method receiver passed as a value, so invoking the method operates on a copy of the receiver.

How it is used
Guides receiver selection when defining methods on Go structures.
How deep it goes
Explains copying, structure size, consistency, and pointer-field concerns, but defers detailed pointer-field behavior.
Examples
Uses the Game type, its mutable state, and its output pointer field to motivate pointer receivers instead.
What you can do after
Apply via Go by selecting value receivers for small structures and pointer receivers when mutation or pointer fields make copying unsuitable.

Read how Learn Go with Pocket-Sized Projects explains value receiver →

Go in Action, Second Edition

A value receiver is a method receiver that operates on a copied value of its receiver type rather than on a pointer to that value.

How it is used
Uses value receivers for conversion methods that return new Measurement values while implementing a shared interface.
How deep it goes
Explains the design rationale and shows conversion implementations, but does not develop broader receiver or pointer semantics.
Examples
Demonstrates value receivers through metric and imperial weight and volume conversion methods.
What you can do after
Build conversion methods that return new values and satisfy a focused Convertible interface.

Read how Go in Action, Second Edition explains value receiver →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.