What if your data could do things by itself, making your code smarter and simpler?
Why methods are used in Go - The Real Reasons
Imagine you have a list of shapes like circles and rectangles, and you want to calculate their areas. Without methods, you have to write separate functions for each shape and pass the shape data every time.
This manual way is slow and confusing because you must remember which function to call and always pass the right data. It's easy to make mistakes and your code becomes messy and hard to manage.
Methods let you attach functions directly to your shapes. This means each shape knows how to calculate its own area. You just call the method on the shape, making your code cleaner, easier to read, and less error-prone.
func areaCircle(c Circle) float64 { return 3.14 * c.radius * c.radius }
area := areaCircle(myCircle)func (c Circle) Area() float64 { return 3.14 * c.radius * c.radius }
area := myCircle.Area()Methods enable your data to carry its own behavior, making programs easier to understand and extend.
Think of a car: instead of having separate functions to start, stop, or honk, the car object itself has these methods, so you just tell the car to start or honk.
Methods connect data and behavior together.
They simplify code by letting objects handle their own actions.
This leads to clearer, more maintainable programs.