# Golang Program For Array Rotation

## Introduction

In this tutorial, we will see to write a go language program to rotate an array. We will write two programs for this. One to rotate the array to left and another to rotate it to right.

## Golang Program to Rotate an Array to Left

The following code illustrates how we can rotate an array to left using a user defined function.

### Algorithm to the above Program

Step 1 − Import the fmt package that allows us to print anything on the screen.

Step 2 − Create a function named rotateLeft() which returns the final array after rotating it.

Step 3 − This function uses a for loop to iterate over the array and calls rotateLeftByOne() function in each iteration.

Step 4 − This function takes the array as an argument and uses for loop to iterate over the array variable.

Step 5 − In each iteration we are placing the next value to previous position and restoring the first element.

Step 6 − Now call the main() function.

Step 7 − Initialize an array of integers and assign values to it.

Step 8 − Print the array on the screen and call the rotateLeft() function by passing the array and number of times the elements should be shifted as arguments to the function.

Step 9 − Print the final array on the screen using fmt.Println() function.

### Example

package main
import "fmt"
func rotateLeft(arr []int, count int) {
for i := 0; i < count; i++ {
rotateLeftByOne(arr)
}
}
func rotateLeftByOne(arr []int) {
var i int = 0
var temp int = arr[0]
for ; i < len(arr)-1; i++ {
arr[i] = arr[i+1]
}
arr[i] = temp
}
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8}
fmt.Println("The entered array is:", arr)
rotateLeft(arr, 7)
fmt.Println("The array obtained by rotating it to left by 7 positions is:", arr)
}


### Output

The entered array is: [1 2 3 4 5 6 7 8]
The array obtained by rotating it to left by 7 positions is: [8 1 2 3 4 5 6 7]


## Golang Program to Rotate an Array to Right

The following code illustrates a go language program to rotate the array elements to right by any number of times.

### Algorithm to the Above Program

Step 1 − Import the fmt package that allows us to print anything on the screen.

Step 2 − Define a function named rotateRight() which will rotate the array to right by any number of times.

Step 3 − It uses a for loop to iterate over the array and stores the second last element in a variable.

Step 4 − it then uses one more for loop to shift the elements to right to the length of the array.

Step 5 − Start the main() function. This is the starting point of the program from where execution starts.

Step 6 − Initialize an array of integers and assign values to it. print this array on the screen.

Step 7 − Now call the rotateRight() function by passing the array and number of times shift should happen as arguments to it.

Step 8 − Print the final array on the screen using fmt.Println() function.

### Example

package main
import "fmt"
func rotateRight(arr []int, count int) {
for i := 0; i < count; i++ {
var j, last int
length := len(arr)
last = arr[length-1]
for j = length - 1; j > 0; j-- {
arr[j] = arr[j-1]
}
arr[0] = last
}
}
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8}

// printing the array on the screen using fmt.Println() function
fmt.Println("The entered array is:", arr)
rotateRight(arr, 7)
fmt.Println("The array obtained by rotating it to right by 7 positions is:", arr)
}


### Output

The entered array is: [1 2 3 4 5 6 7 8]
The array obtained by rotating it to right by 7 positions is: [2 3 4 5 6 7 8 1]


## Rotate the Array to Right by Using Predefined Functions

Let us now look at another program using which we can rotate the array variables to right but without using loops and conditionals.

### Syntax

func copy(dst, str[] type) int


The copy function in go language is used to copy the values of one source array to the destination array and returns the number of elements copied as the result. It takes the two arrays as an argument.

func make ([] type, size, capacity)


The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

### Example

package main
import "fmt"
func rotateRight(nums []int, k int) {
k %= len(nums)
new_array := make([]int, len(nums))
copy(new_array[:k], nums[len(nums)-k:])
copy(new_array[k:], nums[:len(nums)-k])
copy(nums, new_array)
}
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8}
fmt.Println("The entered array is:", arr)
rotateRight(arr, 7)
fmt.Println("The array obtained by rotating it to right by 7 positions is:", arr)
}


### Output

The entered array is: [1 2 3 4 5 6 7 8]
The array obtained by rotating it to right by 7 positions is: [2 3 4 5 6 7 8 1]


## Conclusion

We have successfully compiled and executed a go language program to rotate the elements of an array along with examples.