Types & Interfaces · 9 min read · Intermediate
Structs, Value vs Pointer Receivers
Model domain entities with structs and choose between value receivers and pointer receivers on methods.
What you'll learn
- Define structs with named fields and struct literals
- Attach methods using value and pointer receivers
- Know when a method must use a pointer receiver to mutate state
Overview
Model domain entities with structs and choose between value receivers and pointer receivers on methods.
Go prioritizes simplicity, explicit control flow, fast compilation, and safe concurrency. Every feature in this lesson is part of Go’s minimal standard toolkit used across cloud infrastructure, CLI tools, and high-throughput APIs.
Idiomatic Go Example
package main
import "fmt"
type Counter struct {
Value int
}
func (c *Counter) Increment() {
c.Value++
}
func main() {
c := Counter{Value: 1}
c.Increment()
fmt.Println(c.Value)
}
Key Takeaways
- Define structs with named fields and struct literals
- Attach methods using value and pointer receivers
- Know when a method must use a pointer receiver to mutate state
Quick Check
Check Your Understanding
Score: 0 / 1
1. When should a Go method use a pointer receiver `(c *Counter)` instead of a value receiver?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.