# Go – Move() function of ring package in go

Move() function of ring package is used to move ring element backward (n < 0) or forward (n >= 0) in go where ring must not be empty.

Move() function of ring package moves n % (r.Len()) elements based on input n. If n < 0 then It will move n % (r.Len()) elements backward and if n >= 0 then It will move n % (r.Len()) elements forward.

### Move() function prototype of ring package:

```func (r *Ring) Move(n int) *Ring

Input:
n : Number of elements to be moved either backward or forward based on n value. If n < 0 then n elements will be moved backward else forward.```

#### Return type in Next() function of ring package:

` Move() returns the ring first element pointer after moving the elements.`

### Explanation

```1)
ring r := 0 -> 1 -> 2 -> 3 -> 4 -> 5, It's a circular linked list.

If r = r.Move(3) , n = 3 i.e n >= 0 . So it will move by 3 elements forward. Now elements will be:

r := 3 -> 4 -> 5 -> 0 -> 1 -> 2

2)
ring r := 0 -> 1 -> 2 -> 3 -> 4 -> 5, It's a circular linked list.

If r = r.Move(-4) , n = -4 i.e n < 0 . So it will move by 4 elements backward. Now elements will be:

r := 2 -> 3 -> 4 -> 5 -> 0 -> 1 ```

Example:

```package main

import (
"container/ring"
"fmt"
)

func main() {
// Create a new ring of size 6
r := ring.New(6)

// Get the length of the ring
n := r.Len()

// Initialize the ring with some integer values
for i := 0; i < n; i++ {
r.Value = i
r = r.Next()
}

// Iterate through the ring and print its contents
fmt.Println("Ring elements are: ")
for j := 0; j < n; j++ {
fmt.Println(r.Value)
r = r.Next()
}

// Move the elements by 3 forward
fmt.Println("\nMove the elements by 3 forward")

r = r.Move(3)

// Print ring elements after moving by 3 forward
fmt.Println("Ring elements are: ")
for j := 0; j < n; j++ {
fmt.Println(r.Value)
r = r.Next()
}

// Move the elements by 4 backward
fmt.Println("\nMove the elements by 4 backward")

r = r.Move(-4)

// Print ring elements after moving by 4 backward
fmt.Println("Ring elements are: ")
for j := 0; j < n; j++ {
fmt.Println(r.Value)
r = r.Next()
}

}
```

Output:

*** Here, backward move operation is performed on forwarded moved elements.

```Ring elements are:
0
1
2
3
4
5

Move the elements by 3 forward
Ring elements are:
3
4
5
0
1
2

Move the elements by 4 backward
Ring elements are:
5
0
1
2
3
4```