Introduction
Go (or Golang) is a statically typed compiled programming language, known for its simplicity and performance. One of its elegant features is how it handles functions and methods. While they may look similar at first glance, there are key differences.
In this post we'll explore the following:
- What functions and methods are
- The differences between functions and methods
- When to use one over the other
- Real world code examples
Let's dive in baby πͺ
What is a Function in Golang?
A function in Golang is a standalone piece of code that given an input returns a result. It isn't tied to a specific type.
func Deposit(account BankAccount, amount float64) float64 {
return account.Balance + amount
}
This is a simple function that takes a BankAccount and a deposit amount and returns the new balance, but it does not modify the original account.
What is a Method in Golang?
A method is a function attached to a specific type via a receiver.
Methods can also be called Reciever functions, cuz it's tied to a specific type known as the Reciever type.
func (a *BankAccount) Deposit(amount float64) {
a.Balance += amount
}
Now Deposit() is a method that modifies the original BankAccount.
π‘ Example Code
Hereβs a full Go program demonstrating functions vs methods in a banking context, Feel free to Ctrl+c, Ctrl+v into your IDE and play around:
package main
import (
"fmt"
)
type BankAccount struct {
Owner string
Balance float64
}
type User struct {
FirstName string
LastName string
}
// Function: stand-alone logic
func GetFullName(user User) string {
return user.FirstName + " " + user.LastName
}
// Method: tied to User type
func (u User) FullName() string {
return u.FirstName + " " + u.LastName
}
// Function: calculates future balance (does not change the account)
func Deposit(account BankAccount, amount float64) float64 {
return account.Balance + amount
}
// Method: modifies the account (pointer receiver)
func (a *BankAccount) Deposit(amount float64) {
a.Balance += amount
}
// Method: withdraws money
func (a *BankAccount) Withdraw(amount float64) bool {
if amount > a.Balance {
return false
}
a.Balance -= amount
return true
}
func main() {
// User example
user := User{"John", "Doe"}
fmt.Println("Function:", GetFullName(user))
fmt.Println("Method:", user.FullName())
// BankAccount example
account := BankAccount{Owner: user.FullName(), Balance: 1000}
fmt.Printf("Initial Balance: $%.2f\n", account.Balance)
// Function: simulate deposit (no change to actual account)
newBalance := Deposit(account, 500)
fmt.Printf("Simulated deposit (function): $%.2f\n", newBalance)
fmt.Printf("Actual balance after function: $%.2f\n", account.Balance)
// Method: actual deposit
account.Deposit(500)
fmt.Printf("Balance after method deposit: $%.2f\n", account.Balance)
// Withdraw using method
success := account.Withdraw(200)
fmt.Printf("Withdraw $200 successful? %v. Balance: $%.2f\n", success, account.Balance)
}
Use Functions When β :
- You want pure operations that don't mutate objects
- Youβre writing helper utilities or logic that works across many types
Use Methods When β :
- You want to modify the internal state of an object (e.g., account balance)
- You want cleaner, object-oriented-style calls: account.Withdraw(100)
π Final Thoughts
Understanding the difference between functions and methods in Go is essential for writing clean, idiomatic code.
With these banking examples, you can now decide when to use each β and why it matters!
Top comments (0)