0
0
Goprogramming~20 mins

Why methods are used in Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Methods Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use methods in Go?

Why do programmers use methods in Go instead of just functions?

AMethods are only used to hide code from other programmers.
BMethods run faster than functions because they use special Go hardware instructions.
CMethods automatically make programs run in parallel without extra code.
DMethods allow functions to be associated with specific data types, making code organized and easier to understand.
Attempts:
2 left
💡 Hint

Think about how methods help group actions with the data they work on.

Predict Output
intermediate
2:00remaining
Output of method call on struct

What is the output of this Go program?

Go
package main
import "fmt"
type Person struct {
  name string
}
func (p Person) greet() string {
  return "Hello, " + p.name
}
func main() {
  p := Person{name: "Anna"}
  fmt.Println(p.greet())
}
ACompilation error
BHello,
CHello, Anna
Dp.greet
Attempts:
2 left
💡 Hint

Look at how the method uses the receiver to access the name field.

🔧 Debug
advanced
2:00remaining
Why does this method not modify the struct?

Consider this Go code. Why does the method not change the original struct's field?

Go
package main
import "fmt"
type Counter struct {
  count int
}
func (c Counter) increment() {
  c.count++
}
func main() {
  c := Counter{count: 5}
  c.increment()
  fmt.Println(c.count)
}
ABecause the method has a value receiver, it works on a copy, so the original count is unchanged.
BBecause the increment method is missing a return statement.
CBecause Go does not allow modifying struct fields inside methods.
DBecause the count field is private and cannot be changed.
Attempts:
2 left
💡 Hint

Think about how Go passes the receiver to the method.

📝 Syntax
advanced
2:00remaining
Identify the correct method declaration

Which option shows the correct way to declare a method named describe for a struct type Book in Go?

Afunc (b Book) describe() string { return b.title }
Bfunc describe(b Book) string { return b.title }
Cfunc (Book) describe() string { return title }
Dfunc (b *Book) describe { return b.title }
Attempts:
2 left
💡 Hint

Remember method syntax requires receiver in parentheses and a return type if returning a value.

🚀 Application
expert
3:00remaining
Choosing pointer vs value receiver for methods

Given a struct Account with a method Deposit that adds money to the balance, which method receiver choice is best and why?

AUse a value receiver because it is safer to avoid changing the original data.
BUse a pointer receiver so the method can modify the original Account's balance.
CUse a pointer receiver only if the struct has more than 10 fields.
DUse a value receiver and return the new Account with updated balance.
Attempts:
2 left
💡 Hint

Think about whether the method needs to change the original data or just read it.