0
0
Goprogramming~10 mins

Pointer receivers in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a method with a pointer receiver.

Go
func (p [1] Person) SetName(name string) {
    p.Name = name
}
Drag options to blanks, or click blank then click option'
APerson
Bperson
C&Person
D*Person
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value receiver (Person) which does not modify the original struct.
2fill in blank
medium

Complete the code to call the method with a pointer receiver correctly.

Go
p := Person{Name: "Alice"}
p.[1]("Bob")
Drag options to blanks, or click blank then click option'
Asetname
BSetName
CsetName
DSetname
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect capitalization for the method name.
3fill in blank
hard

Fix the error in the method receiver declaration to allow modifying the struct.

Go
func (p [1] Person) UpdateAge(age int) {
    p.Age = age
}
Drag options to blanks, or click blank then click option'
A*Person
BPerson
C&Person
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value receiver which does not update the original struct.
4fill in blank
hard

Fill both blanks to create a pointer receiver method that increments Age by 1.

Go
func (p [1] Person) [2]() {
    p.Age++
}
Drag options to blanks, or click blank then click option'
A*Person
BPerson
CIncrementAge
DincrementAge
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value receiver which does not modify the original struct.
Using lowercase method name which may not be exported.
5fill in blank
hard

Fill all three blanks to define a pointer receiver method that resets Name and Age.

Go
func (p [1] Person) [2]() {
    p.Name = [3]
    p.Age = 0
}
Drag options to blanks, or click blank then click option'
A*Person
BReset
C""
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value receiver which does not update the original struct.
Forgetting to reset Name to an empty string.