Pointer vs Value

When to Use Pointers in Go

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
}