Menu Close

Go – Prev() function of ring package in go

Prev() function of ring package is used to returns the previous ring element in go where ring must not be empty.

Prev() function prototype of ring package:

func (r *Ring) Prev() *Ring

Return type in Prev() function of ring package:

 Prev() returns the previous ring element. It's a Ring type.

Example of Prev() function in ring package:

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 backwards and print its contents
	for j := 0; j < n; j++ {
		r = r.Prev()
		fmt.Println(r.Value)
	}

}

Output:

5
4
3
2
1
0

To learn more about golang, Please refer given below link:

References:

https://golang.org/doc/

Posted in golang, packages, ring

Leave a Reply

Your email address will not be published. Required fields are marked *