Structs and Methods in Go: Objects Without Classes
Group related fields into Go structs and attach behavior with value and pointer methods—no inheritance headaches required.
You'll learn how Go groups related data into a struct, how methods attach functions to that struct, and why pointer receivers (*User) are needed when a method modifies fields. Assumes basic Go functions and types.
1. Grouping data into a struct
Go has no class keyword, no extends, and no six-level inheritance trees. Instead, Go keeps data and behavior simple with structs.
A struct is a custom blueprint that bundles named fields together:
package main
import "fmt"
type Employee struct {
Name string
Coffees int
}
func main() {
gerald := Employee{Name: "Gerald", Coffees: 2}
fmt.Println(gerald.Name, "has had", gerald.Coffees, "coffees")
}Gerald has had 2 coffeesIf you omit a field (Employee{Name: "Gerald"}), Go sets the missing field to its safe zero value (Coffees: 0).
Notice the capital letters on Employee, Name, and Coffees. In Go, capitalized names are exported (visible to other packages), while lowercase names stay private to your package.
2. Attaching methods to a struct
A method is just a regular function with a special extra argument—the receiver—placed right before the function name:
func (e Employee) Status() string {
return fmt.Sprintf("%s (%d coffees)", e.Name, e.Coffees)
}Now you can call gerald.Status().
Value receiver vs. pointer receiver
Here is the #1 beginner surprise with Go methods. By default, Go passes a copy of gerald into Status(). That is fine when you are only reading fields, but what if you want a method to change gerald.Coffees?
// Value receiver: modifies a COPY, original stays unchanged!
func (e Employee) DrinkCoffeeBroken() {
e.Coffees++
}
// Pointer receiver (*Employee): modifies the ORIGINAL
func (e *Employee) DrinkCoffee() {
e.Coffees++
}gerald := Employee{Name: "Gerald", Coffees: 2}
gerald.DrinkCoffeeBroken()
fmt.Println(gerald.Coffees) // still 2!
gerald.DrinkCoffee()
fmt.Println(gerald.Coffees) // now 3!2
3Run both DrinkCoffeeBroken() and DrinkCoffee() in main.go to see the difference firsthand.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Method changes a field but nothing updates | Used (e Employee) value receiver | Change receiver to (e *Employee) |
Lowercase field names in JSON (name string) | encoding/json ignores unexported fields | Capitalize exported fields (Name string) |
unknown field in struct literal | Typo in field name | Match the exact field spelling in type ... struct |
Practice
- When should a method use a pointer receiver
(u *User)instead of a value receiver(u User)?
Answer: Whenever the method needs to modify fields on the struct (or when the struct is large and you want to avoid copying it).
What you learned
- A
structbundles named fields into a custom type. - Capitalized struct and field names are exported; lowercase names are private.
- Methods use
(e Employee)to read a copy or(e *Employee)to mutate the original struct.