Menu Close

Go – Next() function of ring package in go

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

Next() function prototype of ring package:

func (r *Ring) Next() *Ring

Return type in Next() function of ring package:

 Next() returns the next ring element. It's Ring type.

Example of Next() function in ring package:

package main

import (
	"container/ring"
	"fmt"
)

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

	// 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
	for j := 0; j < n; j++ {
		fmt.Println(r.Value)
		r = r.Next()
	}

}

Output:

0
1
2
3
4
5
6
7

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.

Contact Us