By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A struct is a product type: it has exactly the fields you declare. Methods are functions with a receiver, so behavior lives next to the data it needs without a class hierarchy. Go keeps the receiver explicit because the difference between a value receiver and a pointer receiver is a real difference: one works on a copy, the other can modify what the caller sees.
The rule of thumb: use a pointer receiver when the method mutates the struct, when the struct is large enough that copying it matters, or when the struct contains a mutex that must not be copied. Use a value receiver for small immutable values such as a Severity or a time.Time. Be consistent — mixing receivers on the same type makes the method set hard to reason about.
Embedding a type as an unnamed field promotes its fields and methods to the outer type. That is composition with syntactic sugar: the outer type does not become a subtype, and there is no virtual dispatch. Relay uses it for shared metadata such as request and trace identifiers, so every event carries its correlation IDs without repeating field names.
package main
import "fmt"
type metadata struct {
RequestID string
TraceID string
}
func (m metadata) Label() string {
return "req=" + m.RequestID + " trace=" + m.TraceID
}
type event struct {
metadata
Service string
Status int
duration int
}
func newEvent(service string, status int) event {
return event{
metadata: metadata{RequestID: "req-1", TraceID: "trace-9"},
Service: service,
Status: status,
}
}
func (e event) IsFailure() bool {
return e.Status >= 500
}
func (e *event) SetStatus(status int) {
e.Status = status
}
func (e event) Level() string {
if e.IsFailure() {
return "error"
}
return "info"
}
func (e event) String() string {
return fmt.Sprintf("%s %s %d %s", e.Service, e.Level(), e.Status, e.Label())
}
func main() {
e := newEvent("api", 200)
fmt.Println(e.IsFailure(), e.Level())
e.SetStatus(503)
fmt.Println(e.IsFailure(), e.Level())
fmt.Println(e)
fmt.Printf("%+v\n", e.metadata)
}Two struct habits pay off later. First, keep domain structs free of transport concerns at the edges: an event in the analyzer should not carry an http.Request. Second, use field tags only where they earn their keep — JSON names at the API boundary, database columns in the repository layer — and make constructors explicit when zero values are not safe. Unexported fields (duration above) keep invariants inside the package until you intentionally expose accessors.
| Decision | Prefer | Reason |
|---|---|---|
| Mutating receiver | func (e *event) SetStatus(...) | The caller sees the change |
| Small immutable value | func (s Severity) String() string | Copies are cheap and safe |
| Reuse shared fields | Embedded metadata | Promoted fields, no inheritance |
| Hide an invariant | Unexported field plus method | The package controls mutation |
| Construct a safe value | newEvent(...) constructor | Zero value may be invalid |
| JSON field names | json:"request_id" tag | The wire contract is explicit |
Key takeaways
Structs are values; methods attach behavior through an explicit receiver.
Use pointer receivers to mutate or to avoid copying; value receivers for small immutable values.
Embedding promotes fields and methods and models composition, not inheritance.
Keep transport, storage and domain concerns in separate structs at the edges.
go vet catches copied mutexes and suspicious struct tags — run it.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.