Use pointers in Go to modify data in place or avoid copying large structs, and use values for small, immutable data.
Use pointers in Go when you need to modify a value inside a function, avoid copying large structs, or share state between goroutines. Use values when the data is small, immutable, or needs to be safely copied.
// Use pointer to modify the original struct
func updateConfig(c *Config) {
c.Debug = true
}
// Use value for small, immutable data
func calculateSum(a int, b int) int {
return a + b
}
Use a pointer when you want to change the original data or save memory on large items, like handing someone the address of your house so they can leave a package there. Use a value when you just need to read the data or it's small enough that copying it doesn't matter, like handing someone a photocopy of a business card.