📖 Scenario: Imagine you have a simple program that keeps track of a book's details. You want to organize the code so that actions related to the book, like showing its title or updating its page count, are grouped together. This makes the program easier to understand and use.
🎯 Goal: You will create a Go program that defines a Book type and uses methods to show the book's title and update its page count. This will help you see why methods are useful to group actions with the data they belong to.
📋 What You'll Learn
Create a struct called Book with fields title (string) and pages (int).
Create a method called ShowTitle for Book that prints the book's title.
Create a method called UpdatePages for Book that changes the page count.
Create a main function to create a Book instance, call ShowTitle, update pages, and then print the new page count.
💡 Why This Matters
🌍 Real World
In real programs, methods help keep data and the actions on that data together, like a book knowing how to show its title or update its pages.
💼 Career
Understanding methods is key for writing clean, organized code in Go, which is important for many software development jobs.
Progress0 / 4 steps
1
Create the Book struct
Create a struct called Book with two fields: title of type string and pages of type int.
Go
Hint
Use the type keyword to define a struct. Inside curly braces, list the fields with their types.
2
Add the ShowTitle method
Add a method called ShowTitle with a receiver of type *Book that prints the book's title using fmt.Println. Import fmt package if needed.
Go
Hint
Define a method with func (b *Book) ShowTitle(). Use fmt.Println to print b.title.
3
Add the UpdatePages method
Add a method called UpdatePages with a receiver of type *Book that takes an int parameter called newPages and updates the pages field of the book.
Go
Hint
Define a method with func (b *Book) UpdatePages(newPages int). Inside, set b.pages = newPages.
4
Use the methods in main
In the main function, create a Book variable called myBook with title "Go Basics" and pages 100. Call myBook.ShowTitle(), then call myBook.UpdatePages(150), and finally print the updated pages using fmt.Println(myBook.pages).
Go
Hint
Use myBook := Book{title: "Go Basics", pages: 100} to create the book. Call methods with myBook.ShowTitle() and myBook.UpdatePages(150). Print pages with fmt.Println(myBook.pages).
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
Step 1: Understand the purpose of methods
Methods are functions tied to a specific data type, helping organize code logically.
Step 2: Compare methods with other options
Options A, B, and D describe unrelated or incorrect uses of methods.
Final Answer:
To associate functions with specific data types for better organization -> Option C
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
Step 1: Recall Go method syntax with pointer receiver
The receiver is declared inside parentheses before the method name, with *Type for pointer.
D. Change receiver to pointer: func (r *Rectangle) Scale(factor int) {}
Solution
Step 1: Understand why Scale does not change Rectangle
The receiver is a value copy, so changes inside Scale do not affect original Rectangle.
Step 2: Fix by using pointer receiver
Changing receiver to *Rectangle allows method to modify original struct fields.
Final Answer:
Change receiver to pointer: func (r *Rectangle) Scale(factor int) {} -> Option D
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
Step 1: Understand method purpose and data modification
Updating email requires changing the original struct, so pointer receiver is needed.
Step 2: Combine update and logging logically
Method with pointer receiver can update email and call logging function internally for reuse and clarity.
Final Answer:
Define a pointer receiver method to update email and call a separate logging function inside it -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.