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?
- the ownership of the object remains with the caller
- no dangling reference, and hence no memory leaks
- the caller can choose to shuffle the array of any type
- 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