Pointer receiver (*T) — operates on the original via pointer, can modify it, avoids copying. Value receiver (T) — operates on a copy, changes don't affect the original.
The choice is fundamental: pointer receivers allow mutation and avoid copy overhead, while value receivers provide encapsulation by working on a snapshot.
1type Config struct {2 data map[string]string3}45// Pointer receiver — modifies original6func (c *Config) Set(key, val string) {7 c.data[key] = val8}910// Value receiver — safe read11func (c Config) Get(key string) string {12 return c.data[key]13}1415cfg := Config{data: make(map[string]string)}16cfg.Set("host", "localhost")17fmt.Println(cfg.Get("host")) // localhost
When to use:
Common mistakes: