Bird
Raised Fist0
Goprogramming~20 mins

Why methods are used in Go - Challenge Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. Why do we use methods in Go programming language?
easy
A. To avoid using variables in the program
B. To write code that runs faster than functions
C. To associate functions with specific data types for better organization
D. To create global variables automatically

Solution

  1. Step 1: Understand the purpose of methods

    Methods are functions tied to a specific data type, helping organize code logically.
  2. Step 2: Compare methods with other options

    Options A, B, and D describe unrelated or incorrect uses of methods.
  3. Final Answer:

    To associate functions with specific data types for better organization -> Option C
  4. Quick Check:

    Methods connect actions to types = C [OK]
Hint: Methods link functions to types for clear code structure [OK]
Common Mistakes:
  • Thinking methods make code run faster
  • Confusing methods with variables
  • Believing methods create global variables
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 UpdateName(p Person*, newName string) {}
C. func (p Person) *UpdateName(newName string) {}
D. func *UpdateName(p Person, newName string) {}

Solution

  1. Step 1: Recall Go method syntax with pointer receiver

    The receiver is declared inside parentheses before the method name, with *Type for pointer.
  2. Step 2: Check each option

    func (p *Person) UpdateName(newName string) {} matches correct syntax: func (p *Person) MethodName(params) {}. Others have syntax errors.
  3. Final Answer:

    func (p *Person) UpdateName(newName string) {} -> Option A
  4. Quick Check:

    Pointer receiver syntax = D [OK]
Hint: Pointer receiver syntax: func (p *Type) method() {} [OK]
Common Mistakes:
  • Placing * outside parentheses
  • Using * in parameter list incorrectly
  • Missing parentheses around receiver
3. What will be the output of the following Go code?
package main
import "fmt"

type Counter struct {
    value int
}

func (c *Counter) Increment() {
    c.value++
}

func main() {
    c := Counter{value: 5}
    c.Increment()
    fmt.Println(c.value)
}
medium
A. 6
B. 5
C. 0
D. Compilation error

Solution

  1. Step 1: Understand pointer receiver effect

    The Increment method uses a pointer receiver, so it modifies the original Counter's value.
  2. Step 2: Trace the code execution

    Initial value is 5, Increment adds 1, so value becomes 6, which is printed.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Pointer receiver changes value = 6 [OK]
Hint: Pointer receiver methods modify original data, so value changes [OK]
Common Mistakes:
  • Thinking value stays 5 because method looks like a copy
  • Assuming no output due to missing import
  • Confusing pointer and value receivers
4. Identify the error in this Go method definition and fix it:
type Rectangle struct {
    width, height int
}

func (r Rectangle) Scale(factor int) {
    r.width = r.width * factor
    r.height = r.height * factor
}
medium
A. Change method name to lowercase: func (r Rectangle) scale(factor int) {}
B. Add return type: func (r Rectangle) Scale(factor int) Rectangle {}
C. Remove receiver: func Scale(factor int) {}
D. Change receiver to pointer: func (r *Rectangle) Scale(factor int) {}

Solution

  1. Step 1: Understand why Scale does not change Rectangle

    The receiver is a value copy, so changes inside Scale do not affect original Rectangle.
  2. Step 2: Fix by using pointer receiver

    Changing receiver to *Rectangle allows method to modify original struct fields.
  3. Final Answer:

    Change receiver to pointer: func (r *Rectangle) Scale(factor int) {} -> Option D
  4. Quick Check:

    Pointer receiver needed to modify data = A [OK]
Hint: Use pointer receiver to modify struct fields inside methods [OK]
Common Mistakes:
  • Using value receiver when modification is needed
  • Adding return type unnecessarily
  • Changing method name case without reason
5. You want to create a method that updates a user's email in a struct and also logs the change. Which approach best uses methods to achieve this in Go?
hard
A. Define a value receiver method that returns a new struct with updated email and logs externally
B. Define a pointer receiver method to update email and call a separate logging function inside it
C. Use a global function to update email and logging without methods
D. Define a method without receiver that updates email and logs

Solution

  1. Step 1: Understand method purpose and data modification

    Updating email requires changing the original struct, so pointer receiver is needed.
  2. Step 2: Combine update and logging logically

    Method with pointer receiver can update email and call logging function internally for reuse and clarity.
  3. Final Answer:

    Define a pointer receiver method to update email and call a separate logging function inside it -> Option B
  4. Quick Check:

    Pointer receiver + internal logging = A [OK]
Hint: Use pointer receiver method to update and log together [OK]
Common Mistakes:
  • Using value receiver and expecting original to change
  • Separating update and logging outside methods unnecessarily
  • Avoiding methods and using only global functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard