0
0
Goprogramming~10 mins

Receiver types 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]) Greet() string {
    return "Hello, " + p.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 when a pointer receiver is needed.
Forgetting the asterisk (*) in the receiver type.
2fill in blank
medium

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

Go
p := Person{name: "Alice"}
result := p.[1]()
Drag options to blanks, or click blank then click option'
AGreet()
B&Greet
C*Greet
DGreet
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the method with & or * symbols.
Adding parentheses in the method name inside the call.
3fill in blank
hard

Fix the error in the method receiver declaration.

Go
func ([1] Person) UpdateName(newName string) {
    p.name = newName
}
Drag options to blanks, or click blank then click option'
Ap
B*p
CPerson
D*Person
Attempts:
3 left
💡 Hint
Common Mistakes
Using the type name instead of a variable name as receiver.
Using pointer syntax incorrectly in the receiver variable name.
4fill in blank
hard

Fill both blanks to define a method with a pointer receiver and update the struct field.

Go
func ([1] [2]) UpdateAge(newAge int) {
    p.age = newAge
}
Drag options to blanks, or click blank then click option'
Ap
BPerson
C*Person
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value receiver when a pointer receiver is needed to modify fields.
Using the struct name as the receiver variable.
5fill in blank
hard

Fill all three blanks to create a method with a value receiver that returns the person's name in uppercase.

Go
func ([1] [2]) NameUpper() string {
    return strings.[3](p.name)
}
Drag options to blanks, or click blank then click option'
Ap
BPerson
CToUpper
DToLower
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer receiver when value receiver is intended.
Using ToLower instead of ToUpper.
Incorrect receiver variable name.