Skip to content

Instantly share code, notes, and snippets.

@hartfordfive
Last active January 1, 2023 17:58
Show Gist options
  • Save hartfordfive/77f2e6de9abd3be4ba66123b9e526f0f to your computer and use it in GitHub Desktop.
Save hartfordfive/77f2e6de9abd3be4ba66123b9e526f0f to your computer and use it in GitHub Desktop.
Simple Go circular list using the ring package
package main
// Run at: https://play.golang.org/p/waIvysrhXJ
import (
"container/ring"
"fmt"
)
type CircularList struct {
list *ring.Ring
}
func NewCircularList(items ...interface{}) *CircularList {
cl := &CircularList{
list: ring.New(len(items)),
}
for i := 0; i < cl.list.Len(); i++ {
cl.list.Value = items[i]
cl.list = cl.list.Next()
}
return cl
}
func (cl *CircularList) ShowAll() {
cl.list.Do(func(x interface{}) {
fmt.Printf("Item: %v\n", x)
})
}
func (cl *CircularList) GetItem() interface{} {
val := cl.list.Value
cl.list = cl.list.Next()
return val
}
func main() {
cl := NewCircularList("win", "loss", "tie")
for i := 0; i < 5; i ++ {
fmt.Printf("Iteration #%d is: %v\n", i, cl.GetItem())
}
fmt.Println("----------")
cl2 := NewCircularList(0,1,2,3,5,8)
for i := 0; i < 10; i ++ {
fmt.Printf("Iteration #%d is: %v\n", i, cl2.GetItem())
}
fmt.Println("----------")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment