Memory/Borrowing
6 articles
Common Slice Gotchas in Go: Shared Backing Arrays and Memory Leaks
Fix Go slice memory leaks by always assigning the return value of functions like slices.Delete to update the slice header.
How Slice Internals Work: Length, Capacity, and Underlying Arrays
Go slices manage dynamic arrays by tracking current length and total capacity, automatically resizing when needed.
The Append Gotcha: When Append Modifies the Original Slice
Prevent `append` from modifying the original slice by cloning it first to ensure a separate underlying array.
The Shared Slice Backing Array Gotcha in Go
The shared slice backing array occurs when a slice is created from a larger array (like a buffer) and modified, causing changes to reflect in the original array because they share the same underlying memory.
Using Struct Value in Map: "cannot assign to struct field in map"
Fix the 'cannot assign to struct field in map' error by retrieving the struct, modifying it, and reassigning it to the map key.
Why You Can't Take the Address of a Map Value in Go
Map lookups in Go return value copies, not references, so you must use pointer types or reassign values to modify map data.