0
0
Goprogramming~10 mins

Value 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 value receiver.

Go
type Counter struct {
    count int
}

func (c Counter) Increment() {
    c.count++
}
Drag options to blanks, or click blank then click option'
A*
BCounter
Cc
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using * or & as the receiver variable name.
Using the type name instead of a variable name.
2fill in blank
medium

Complete the code to call the Increment method on a value receiver.

Go
c := Counter{count: 0}
c.[1]()
fmt.Println(c.count)
Drag options to blanks, or click blank then click option'
AincrementCount
BIncrement
CAdd
Dincrement
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase method names that do not exist.
Calling a method that is not defined.
3fill in blank
hard

Fix the error in the method receiver declaration.

Go
func (c *Counter) Increment() {
    c.count++
}
Drag options to blanks, or click blank then click option'
ACounter
B&c
C*c
Dc
Attempts:
3 left
💡 Hint
Common Mistakes
Using the type name as the receiver variable.
Using pointer dereference syntax in the receiver variable.
4fill in blank
hard

Fill both blanks to define a method with a value receiver and print the count.

Go
func (c Counter) PrintCount() {
    fmt.Println(c.count)
}
Drag options to blanks, or click blank then click option'
Ac
BCounter
Ccount
D*Counter
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer type for value receiver.
Using wrong variable names.
5fill in blank
hard

Fill all three blanks to create a value receiver method that increments count and prints it.

Go
func (c Counter) IncrementAndPrint() {
    c.count++
    fmt.Println(c.count)
}
Drag options to blanks, or click blank then click option'
Ac
BCounter
Ccount
D*Counter
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer type for value receiver.
Using wrong field name.
Using wrong variable name.