A method is a function that is attached to a specific type.
In Go, methods are commonly used with structs to define behavior.
Methods are called using the dot (.) operator on a variable.
This works similarly to calling methods on objects in other languages.
A regular function is not attached to any type, while a method belongs to a specific type.
Methods allow you to group behavior with data.
A value receiver creates a copy of the struct when the method is called.
Changes inside the method do not affect the original value.
A pointer receiver allows the method to modify the original struct.
This is commonly used when you want to update values.
Pointer receivers are preferred in most real-world cases.
A struct can have multiple methods, allowing it to have different behaviors.
Methods are commonly used to model behavior in applications.
Be careful with these:
Create a Car struct with a method that prints its brand.