Methods

This Go program demonstrates the usage of methods with both value receivers and pointer receivers on a struct.

Let's go through each part of the code with inline comments and additional explanations:

package main

import "fmt"

// Define a struct named 'rect' with two fields: 'width' and 'height'
type rect struct {
    width, height int
}

// Method with a pointer receiver: 'area' calculates and returns the area of the rectangle
func (r *rect) area() int {
    return r.width * r.height
}

// Method with a value receiver: 'perim' calculates and returns the perimeter of the rectangle
func (r rect) perim() int {
    return 2*r.width + 2*r.height
}

func main() {
    // Create an instance of the 'rect' struct with field values 10 and 5
    r := rect{width: 10, height: 5}

    // Call the 'area' method on the struct and print the result
    fmt.Println("area: ", r.area()) // Output: area: 50

    // Call the 'perim' method on the struct and print the result
    fmt.Println("perim:", r.perim()) // Output: perim: 30

    // Create a pointer to the 'rect' struct
    rp := &r

    // Call the 'area' method using a pointer receiver and print the result
    fmt.Println("area: ", rp.area()) // Output: area: 50

    // Call the 'perim' method using a value receiver through the pointer and print the result
    fmt.Println("perim:", rp.perim()) // Output: perim: 30
}

Output

area:  50
perim: 30
area:  50
perim: 30

Explanation:

  1. A rect struct is defined with two fields: width and height.

  2. The area method is defined with a pointer receiver (*rect). It calculates and returns the area of the rectangle.

  3. The perim method is defined with a value receiver (rect). It calculates and returns the perimeter of the rectangle.

  4. In the main function:

    • An instance r of the rect struct is created with field values 10 and 5.

    • The area and perim methods are called on the struct, and the results are printed.

    • A pointer rp to the rect struct is created.

    • The area and perim methods are called using the pointer, and the results are printed.

Using a pointer receiver in a method allows modifications to the original struct, while using a value receiver creates a copy of the struct for the method to work with, leaving the original unchanged.

Last updated