We constantly use functions in our code, and, most of the time, they have parameters. Let’s imagine we’re writing code for the bowling alley that opened last week. We need to print the scores on the screen after a player’s turn. We write the function shown in the following listing.
Listing E.1 Showing a player’s score
This does the job, so we’re happy. However, we notice that the function updating the score doesn’t seem to work.
Listing E.2 Updating the player’s score
func AddPoints(p Player, points int) { p.score += points }
After writing a simple test, we realize that the player’s score after calling AddPoints
didn’t change! The main reason behind this lies with Go’s handling of parameters in functions known as passing parameters by value and passing parameters by reference.