How to Use container/ring in Go

The `container/ring` package provides a circular linked list data structure for storing elements in a fixed-size loop. Create a new ring with `New(n)`, set the `Value` field of each node to store data, and use `Next()` or `Prev()` to traverse the list.

How to Use container/ring in Go

The container/ring package provides a circular linked list data structure for storing elements in a fixed-size loop. Create a new ring with New(n), set the Value field of each node to store data, and use Next() or Prev() to traverse the list.

package main

import (
	"fmt"
	"container/ring"
)

func main() {
	r := ring.New(3)
	r.Value = "first"
	r = r.Next()
	r.Value = "second"
	r = r.Next()
	r.Value = "third"

	for p := r; ; p = p.Next() {
		fmt.Println(p.Value)
		if p == r {
			break
		}
	}
}