To reverse an array in Go, you can loop through the array from both ends and swap their elements until both the ends meet in the middle. Here's an implementation:
main.go168 chars8 lines
This function takes an integer array as input and returns the reversed integer array. We loop through the array until the middle is reached (when i >= len(arr)/2
). At each iteration, we swap the elements at indices i
and j
(which starts at len(arr) - i - 1
). Once the loop finishes, we return the original array arr
, which is now reversed.
gistlibby LogSnag