0
0
Goprogramming~5 mins

Passing values vs pointers in Go - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What happens when you pass a value to a function in Go?
The function receives a copy of the value. Changes inside the function do not affect the original variable.
Click to reveal answer
beginner
What is a pointer in Go?
A pointer holds the memory address of a variable, allowing functions to access and modify the original variable directly.
Click to reveal answer
intermediate
How do you declare a pointer parameter in a Go function?
Use an asterisk (*) before the type in the function parameter, for example: func f(p *int) {}
Click to reveal answer
intermediate
Why use pointers instead of passing values?
Pointers allow functions to modify the original data and can be more efficient for large data structures because they avoid copying.
Click to reveal answer
beginner
What does the & operator do in Go?
It returns the memory address of a variable, creating a pointer to that variable.
Click to reveal answer
What does passing a value to a function in Go do?
APasses the variable name
BPasses the memory address
CPasses a reference to the value
DPasses a copy of the value
How do you pass a pointer to a function in Go?
AUse the & operator to get the address and * in the parameter
BJust pass the variable name
CUse the * operator to get the address
DUse the & operator in the parameter only
What does the * operator do when used with a pointer in Go?
ACreates a new pointer
BReturns the address of the pointer
CDereferences the pointer to access the value
DCopies the pointer value
Why might you prefer passing a pointer over a value?
ATo make a copy of the variable
BTo modify the original variable inside the function
CTo avoid using memory
DTo prevent changes to the variable
What happens if you change a value inside a function that received a copy?
AThe original variable stays the same
BThe original variable changes
CThe program crashes
DThe copy changes the original automatically
Explain the difference between passing a value and passing a pointer to a function in Go.
Think about what happens inside the function when you change the parameter.
You got /4 concepts.
    Describe how to declare and use a pointer parameter in a Go function.
    Remember the & and * operators and their roles.
    You got /4 concepts.