Go Methods

A method is a function that belongs to a specific type. It is defined with a receiver — the type it is attached to. Methods allow custom types like structs to have behavior, making the code more organized and readable.

Defining a Method

A method looks like a regular function but has a receiver between the func keyword and the method name.

package main

import "fmt"

type Circle struct {
    Radius float64
}

func (c Circle) Area() float64 {
    return 3.14159 * c.Radius * c.Radius
}

func main() {
    c := Circle{Radius: 5}
    fmt.Printf("Area: %.2f\n", c.Area()) // Area: 78.54
}

Method Anatomy Diagram

func  (c Circle)  Area()  float64  {
 │        │          │        │
 │        │          │        └── return type
 │        │          └─────────── method name
 │        └────────────────────── receiver: (variableName TypeName)
 └─────────────────────────────── func keyword

Value Receiver vs Pointer Receiver

Value Receiver

A value receiver receives a copy of the struct. Changes inside the method do not affect the original.

func (c Circle) Describe() string {
    return fmt.Sprintf("Circle with radius %.1f", c.Radius)
}

Pointer Receiver

A pointer receiver receives the memory address of the struct. Changes inside the method affect the original.

package main

import "fmt"

type BankAccount struct {
    Balance float64
}

func (b *BankAccount) Deposit(amount float64) {
    b.Balance += amount
}

func (b *BankAccount) Withdraw(amount float64) {
    if amount <= b.Balance {
        b.Balance -= amount
    }
}

func main() {
    account := BankAccount{Balance: 1000}
    account.Deposit(500)
    account.Withdraw(200)
    fmt.Printf("Balance: %.2f\n", account.Balance) // Balance: 1300.00
}

Value vs Pointer Receiver Comparison

FeatureValue ReceiverPointer Receiver
ReceivesA copy of the structA reference to the original
Modifies original?NoYes
Syntax(c Circle)(c *Circle)
Use whenReading data onlyUpdating fields in the struct

Multiple Methods on the Same Type

package main

import "fmt"

type Rectangle struct {
    Width  float64
    Height float64
}

func (r Rectangle) Area() float64 {
    return r.Width * r.Height
}

func (r Rectangle) Perimeter() float64 {
    return 2 * (r.Width + r.Height)
}

func (r Rectangle) IsSquare() bool {
    return r.Width == r.Height
}

func main() {
    rect := Rectangle{Width: 8, Height: 4}
    fmt.Println("Area:", rect.Area())           // Area: 32
    fmt.Println("Perimeter:", rect.Perimeter()) // Perimeter: 24
    fmt.Println("Is Square:", rect.IsSquare())  // Is Square: false
}

Methods on Non-Struct Types

Methods can be attached to any type defined in the same package, not only structs.

package main

import "fmt"

type Celsius float64

func (c Celsius) ToFahrenheit() float64 {
    return float64(c)*9/5 + 32
}

func main() {
    temp := Celsius(100)
    fmt.Println(temp.ToFahrenheit()) // 212
}

Key Points

  • A method is a function with a receiver — the type it belongs to
  • Value receivers get a copy; pointer receivers get the original
  • Use pointer receivers when the method needs to modify the struct
  • Multiple methods can be attached to the same type
  • Methods can be defined on any locally defined type, not just structs

Leave a Comment