Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousStructsNextInterfaces

Last updated 1 year ago

Was this helpful?