What Is the Difference Between a Function and a Method in Go

Functions are standalone code blocks, while methods are functions bound to a specific type via a receiver.

A function is a standalone block of code, while a method is a function attached to a specific type (receiver). Functions are called by name, whereas methods are called on an instance of their type using dot notation.

// Function: Standalone
func add(a, b int) int {
    return a + b
}

// Method: Attached to type 'Calculator'
type Calculator struct{}

func (c Calculator) multiply(a, b int) int {
    return a * b
}

// Usage
result1 := add(2, 3)          // 5
var calc Calculator
result2 := calc.multiply(2, 3) // 6