liveBook · Manning

Call by value

As explained in Go by Example

Pass-by-value is a parameter-passing semantics in which a function receives a copy of each argument's value.

Call by value gives a function its own copy of each argument’s value. In Go, this choice is explicit: a function can receive either a value such as Result or a pointer such as *Result. section 6.3.3

Passing Result values is useful when:

  • The function does not need to mutate the result. section 6.3.3
  • Independent goroutines should work with separate copies instead of shared state. section 6.3.3
  • Large result streams make pointer allocation and garbage-collector tracking costly. section 6.3.3

Results through a synchronous API

Send returns a Result, SendN passes Result values to yield, and Summarize receives Results while iterating over each Result. These operations copy values rather than sharing pointers, supporting predictable processing of performance measurements. section 6.3.3

How Go by Example covers it

How deep it goes
Explains value-versus-pointer semantics, mutation, goroutine isolation, allocation, stack use, caching, and garbage-collector trade-offs.
Examples
Uses Result, Send, SendN, yield, Results, and Summarize to demonstrate copied values in a result-processing pipeline.
What you can do after
Evaluate whether functions should receive Result values or pointers based on mutation, isolation, allocation, and garbage-collection concerns.
How it connects
Connects call by value with pointers, structs, goroutines, shared state, stack allocation, heap allocation, and garbage collection.

Also explained in

Compare how 3 books explain call by value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.