100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Methods in Go

Learn how to attach methods to types in Go using value and pointer receivers.

FunctionsIntermediate8 min readJul 8, 2026
Analogies

Introduction

Go does not have classes, but it lets you attach methods to any named type using a special receiver argument. A method is simply a function with an extra parameter, called the receiver, placed between the func keyword and the method name. This lets types defined in your package expose behavior directly, similar in spirit to methods in object-oriented languages.

🏏

Cricket analogy: Like attaching a strike-rate calculation directly to a Batsman type via a receiver, so calling batsman.StrikeRate() reads naturally, similar to how OOP languages bundle behavior with the batting stats object.

Syntax

go
type Rectangle struct {
    Width, Height float64
}

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

func (r *Rectangle) Scale(factor float64) {
    r.Width *= factor
    r.Height *= factor
}

Explanation

The receiver (r Rectangle) in Area is a value receiver: it receives a copy of the Rectangle, so calling Area cannot modify the original struct. The receiver (r *Rectangle) in Scale is a pointer receiver: it receives a pointer to the original struct, so mutations inside Scale affect the caller's value directly and avoid copying the whole struct. Value receivers are appropriate for read-only methods or small types, while pointer receivers are preferred when the method needs to mutate the receiver or when the type is large and copying would be wasteful. Go automatically takes the address of an addressable value when calling a pointer-receiver method, so r.Scale(2) works even if r is not a pointer variable.

🏏

Cricket analogy: Like handing a scorekeeper a photocopy of the scoreboard to calculate the average with a value receiver versus handing them the actual scoreboard to update after a six with a pointer receiver, Go auto-lets r.Scale(2) work on the original board.

Example

go
package main

import "fmt"

type Rectangle struct {
    Width, Height float64
}

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

func (r *Rectangle) Scale(factor float64) {
    r.Width *= factor
    r.Height *= factor
}

func main() {
    rect := Rectangle{Width: 3, Height: 4}
    fmt.Println("Area:", rect.Area())

    rect.Scale(2)
    fmt.Println("After scale:", rect.Width, rect.Height)
    fmt.Println("New area:", rect.Area())
}

// Output:
// Area: 12
// After scale: 6 8
// New area: 48

Key Takeaways

  • A method is a function with a receiver argument written between func and the method name.
  • Methods can be attached to any named type defined in the same package, not just structs.
  • Value receivers operate on a copy and cannot modify the original data.
  • Pointer receivers operate on the original data and can mutate it, avoiding unnecessary copying.
  • Go has no classes; methods on named types provide similar organization of behavior and data.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#MethodsInGo#Methods#Syntax#Explanation#Example#Functions#StudyNotes#SkillVeris