> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/20-methods.md).

# 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:

```go
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/20-methods.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
