Golang is beautiful and here's an example of its well-designed

Arpit Bhayani

Arpit Bhayani

Oct 25, 2023 • 1 min read


Golang is beautiful and here’s an example of its well-designed method interface ⚡

To shuffle an array (slice) there is an inbuilt function named Shuffle. Counter-intuitively, the function does not require us to pass the array as an argument 🤔

Instead, it expects us to pass the length of the array and a swapping function, that the caller needs to write. The function should swap two values given their indexes.

⚡ Why is this amazing?

  1. the ownership of the object remains with the caller
  2. no dangling reference, and hence no memory leaks
  3. the caller can choose to shuffle the array of any type
  4. no overhead of abstractions like generics/templates/reflections

One of the main reasons why I prefer Golang over any and every language; it is just so elegant ⚡

I keep writing and sharing these engineering nuggets, so if you are keen on learning them, follow along.

youtube.com/c/ArpitBhayani

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses