Methods
A method is like a function, but it’s attached to a specific type (usually a struct). The type becomes the receiver of the method.
func (receiver TypeName) methodName(parameters) returnType {
// method body
}
- Receiver → variable that represents the type the method belongs to.
- You can use value receivers (
t TypeName) or pointer receivers (t *TypeName).
Example of Method
type Rectangle struct {
width, height int
}
// Method with value receiver
func (r Rectangle) area() int {
return r.width * r.height
}
func main() {
rect := Rectangle{width: 10, height: 5}
fmt.Println("Area:", rect.area()) // calling the method
}
Pointer Receiver Example (Modifying data)
func (r *Rectangle) resize(newWidth, newHeight int) {
r.width = newWidth
r.height = newHeight
}
func main() {
rect := Rectangle{width: 10, height: 5}
rect.resize(20, 15) // modifies the original struct
fmt.Println("New Area:", rect.area())
}
Functions + Methods:
// Define a struct (custom type)
type Rectangle struct {
width, height int
}
// Method: calculate area (belongs to Rectangle)
func (r Rectangle) area() int {
return r.width * r.height
}
// Method: resize the rectangle (pointer receiver so it changes the original)
func (r *Rectangle) resize(newWidth, newHeight int) {
r.width = newWidth
r.height = newHeight
}
// Function: compare areas of two rectangles (not tied to Rectangle)
func compareArea(r1, r2 Rectangle) string {
if r1.area() > r2.area() {
return "First rectangle is larger."
} else if r1.area() < r2.area() {
return "Second rectangle is larger."
}
return "Both rectangles have the same area."
}
func main() {
// Create two rectangles
rect1 := Rectangle{width: 10, height: 5}
rect2 := Rectangle{width: 8, height: 6}
fmt.Println("Rectangle 1 area:", rect1.area()) // method call
fmt.Println("Rectangle 2 area:", rect2.area()) // method call
// Compare using a function
fmt.Println(compareArea(rect1, rect2))
// Resize rect1
rect1.resize(12, 7)
fmt.Println("Rectangle 1 new area:", rect1.area())
// Compare again
fmt.Println(compareArea(rect1, rect2))
}
- We define a struct
Rectangle→ this represents the shape with width and height. - We attach two methods:
area()→ calculates and returns the rectangle’s area.resize()→ changes the rectangle’s width and height.
- We write a normal function
compareArea()that works with any rectangles given to it. - In
main():- We create two rectangles.
- We call methods (
areaandresize) directly on them. - We call the function
compareArea()to compare their sizes.
Function vs Method
| Feature | Function | Method |
|---|---|---|
| Belongs to | Independent, not tied to a type | Tied to a specific type (via receiver) |
| Syntax | func name(...) ... {} | func (receiver Type) name(...) ... {} |
| Used for | General tasks | Behavior related to a specific type |
| Can modify data? | Only if passed a pointer to data | Yes, if receiver is a pointer |
- Use functions for general-purpose logic.
- Use methods when you want to define behavior for a type (like attaching abilities to an object).