ring

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 1 Imported by: 1

README

[!CAUTION] github.com/ecnepsnai/ring is deprecated and replaced by git.ecn.io/ian/ring. All users should migrate to git.ecn.io/ian/ring for continued updates. Tag v1.0.0 is drop-in compatible copy of the last release of github.com/ecnepsnai/ring.

Ring

Go Report Card Godoc Releases LICENSE

Package ring is a threadsafe rotating slice. You define a maximum number of items and for each added item, it will fill the slice up to that max, then begin to wrap around the start of the slice.

Usage & Examples

Examples can be found on the documentation for the library

Documentation

Overview

Package ring is a threadsafe rotating slice. You define a maximum number of items and for each added item, it will fill the slice up to that max, then begin to wrap around the start of the slice.

Deprecated: github.com/ecnepsnai/ring is deprecated and replaced by git.ecn.io/ian/ring. All users should migrate to git.ecn.io/ian/ring for continued updates. Tag v1.0.0 is drop-in compatible copy of the last release of github.com/ecnepsnai/ring.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ring

type Ring struct {
	// contains filtered or unexported fields
}

Ring describes a single ring instance

func New

func New(Maximum int) *Ring

New will create a new ring with the specified maximum number of items

Example
package main

import (
	"fmt"

	"github.com/ecnepsnai/ring"
)

func main() {
	// Make a new ring
	list := ring.New(5)

	// Add something to it
	list.Add("foo")

	// Get the last object added
	fmt.Printf("%s", list.Last())
}
Output:
foo

func (*Ring) Add

func (r *Ring) Add(object interface{})

Add will add a new object to the ring

Example
package main

import (
	"fmt"

	"github.com/ecnepsnai/ring"
)

func main() {
	// Make a new ring
	list := ring.New(5)

	// Add something to it
	list.Add("foo")

	// Get the last object added
	fmt.Printf("%s", list.Last())
}
Output:
foo

func (*Ring) All

func (r *Ring) All() []interface{}

All will return a copy of all objects in the ring sorted by when they were added

Example
package main

import (
	"fmt"

	"github.com/ecnepsnai/ring"
)

func main() {
	// Make a new ring with a maximum capacity of 5 objects
	list := ring.New(5)

	// Add 10 objects (0 through 9)
	i := 0
	for i < 10 {
		list.Add(i)
		i++
	}

	// Get all of the objects form the ring, which will be the last 5 numbers
	// we added
	last5 := list.All()
	fmt.Printf("%v", last5)
}
Output:
[5 6 7 8 9]

func (*Ring) Last

func (r *Ring) Last() interface{}

Last will return the last inserted object in the ring, or nil if the ring is empty

Example
package main

import (
	"fmt"

	"github.com/ecnepsnai/ring"
)

func main() {
	// Make a new ring
	list := ring.New(5)

	// Add something to it
	list.Add("foo")

	// Get the last object added
	fmt.Printf("%s", list.Last())
}
Output:
foo

func (*Ring) Truncate added in v1.0.4

func (r *Ring) Truncate()

Truncate will remove all objects from the ring

Example
package main

import (
	"fmt"

	"github.com/ecnepsnai/ring"
)

func main() {
	// Make a new ring
	list := ring.New(5)

	// Add 10 objects (0 through 9)
	i := 0
	for i < 10 {
		list.Add(i)
		i++
	}

	// Get the number of objects in the ring
	beforeCount := len(list.All())

	// Truncate the ring
	list.Truncate()

	// Get the number of objects in the ring
	afterCount := len(list.All())

	fmt.Printf("Before: %d, After: %d", beforeCount, afterCount)
}
Output:
Before: 5, After: 0

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL