GoLang — Optimisations
Aug 20, 2023
Go- Arrays:
- When iterating over an array it’s faster to pass a pointer to that array to the
for
loop. This prevents the loop from copying the whole array which is beneficial for both CPU and memory usage.
Reference: https://medium.com/@haaawk/i-thought-i-understood-how-iteration-over-an-array-works-but-apparently-not-in-golang-441a7abd6540 - If we want to modify the array while we’re iterating over it, we need to either give a
for
loop a pointer to that array or use the array index operator directly.