Why methods are used
📖 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