A method in Go is a function with a special receiver argument. The receiver appears in its own argument list between the func keyword and the method name. Methods allow you to define behavior on types.
Click to reveal answer
intermediate
What is the difference between a value receiver and a pointer receiver in Go methods?
A value receiver gets a copy of the value, so changes inside the method do not affect the original. A pointer receiver gets the address, so changes inside the method can modify the original value.
Click to reveal answer
intermediate
How does Go decide which method to call when both pointer and value receivers exist?
Go automatically takes the address (&) of addressable values for pointer receiver methods and dereferences (*) pointers for value receiver methods, selecting the matching method based on the receiver expression type after conversion.
Click to reveal answer
intermediate
What happens if you call a method with a pointer receiver on a value variable?
Go automatically takes the address of the value variable to call the pointer receiver method, so the call works as if you passed a pointer.
Click to reveal answer
intermediate
Why might you choose a pointer receiver over a value receiver for a method?
You choose a pointer receiver to avoid copying large structs, to modify the original value, or to ensure consistency if some methods require pointers.
Click to reveal answer
In Go, which receiver type allows a method to modify the original struct?
APointer receiver
BValue receiver
CInterface receiver
DConstant receiver
✗ Incorrect
Pointer receivers get the address of the struct, so methods can modify the original value.
What happens if you call a value receiver method on a pointer variable in Go?
AIt causes a compile error
BThe method is ignored
CGo automatically dereferences the pointer
DYou must manually dereference the pointer
✗ Incorrect
Go automatically dereferences pointers when calling value receiver methods.
Which of these is true about method calls in Go?
AMethods with value receivers require explicit pointer dereferencing
BMethods with pointer receivers can be called on values without pointers
CMethods with value receivers can only be called on values
DMethods with pointer receivers can only be called on pointers
✗ Incorrect
Go allows calling methods with pointer receivers on values by automatically taking the address.
Why might Go automatically take the address of a value when calling a method?
ATo optimize memory usage
BTo convert values to interfaces
CTo prevent method calls on values
DTo allow calling pointer receiver methods on values
✗ Incorrect
Go helps by automatically taking the address so pointer receiver methods can be called on values.
Which receiver type copies the struct when calling a method?
AValue receiver
BPointer receiver
CInterface receiver
DReference receiver
✗ Incorrect
Value receivers get a copy of the struct, so the original is not changed.
Explain how Go handles method calls differently for pointer receivers versus value receivers.
Think about how Go treats the receiver when calling the method.
You got /4 concepts.
Describe when and why you would use a pointer receiver for a method in Go.
Consider performance and data modification needs.
You got /4 concepts.
Practice
(1/5)
1. In Go, what happens when you call a method with a value receiver on a variable?
easy
A. The method can only read but not access the variable's fields.
B. The method works on a copy of the variable, so original data is not changed.
C. The method call causes a compile-time error.
D. The method directly modifies the original variable.
Solution
Step 1: Understand value receiver behavior
Methods with value receivers get a copy of the variable, not the original.
Step 2: Effect on original variable
Since the method works on a copy, changes inside the method do not affect the original variable.
Final Answer:
The method works on a copy of the variable, so original data is not changed. -> Option B
Quick Check:
Value receiver = copy, no original change [OK]
Hint: Value receivers work on copies, so original stays unchanged [OK]
Common Mistakes:
Thinking value receivers modify original data
Confusing pointer and value receivers
Assuming method call causes error
2. Which of the following is the correct syntax to define a method with a pointer receiver in Go?
easy
A. func (p *Person) UpdateName(newName string) {}
B. func (p Person*) UpdateName(newName string) {}
C. func (p Person) *UpdateName(newName string) {}
D. func *Person (p) UpdateName(newName string) {}
Solution
Step 1: Recall pointer receiver syntax
Pointer receivers are declared as (variable *Type) before method name.