Sobes.tech
Junior — Middle

How does passing small and large slices to a function affect performance and memory usage?

sobes.tech AI

Answer from AI

In Go, a slice is a structure containing a pointer to an array, a length, and a capacity. When passing a slice to a function, only this structure (about 24 bytes) is copied, not the entire array.

  • For small slices (a few elements), copying the structure has little impact on performance.
  • For large slices, only the structure is copied, not all the data, so memory and time costs are minimal.

Thus, passing a slice is efficient regardless of size, as data is not copied but a reference to the array is passed.