The following list includes common methods. Documentation updates may lag behind code features. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/container/gring
Constructor Methods
New
- Description:
Newcreates and returns a ring structure withcapelements. Optional parametersafespecifies whether to use this structure in concurrency-safe mode, defaults tofalse. - Format:
New(cap int, safe ...bool) *Ring
- Example:
func ExampleNew() {
// Non concurrent safety
gring.New(10)
// Concurrent safety
gring.New(10, true)
// Output:
}
NewTRing
tip
Version requirement: v2.10.0
- Description:
NewTRingcreates and returns a generic ring structure. Optional parametersafespecifies whether to use this structure in concurrency-safe mode, defaults tofalse. - Format:
NewTRing[T any](cap int, safe ...bool) *TRing[T]
- Example:
func ExampleNewTRing() {
// Create a non-concurrent-safe ring with type int
r1 := gring.NewTRing[int](10)
// Create a concurrent-safe ring with type string
r2 := gring.NewTRing[string](10, true)
// Create a ring with custom type
type User struct {
ID int
Name string
}
r3 := gring.NewTRing[*User](10)
// Output:
}
Basic Methods
Val
- Description:
Valreturns the value of the item at the current position. - Format:
// Traditional approach
Val() interface{}
// Generic approach (v2.10.0+)
Val() T
- Example:
func ExampleRing_Val() {
r := gring.New(10)
r.Set(1)
fmt.Println("Val:", r.Val())
r.Next().Set("GoFrame")
fmt.Println("Val:", r.Val())
// Output:
// Val: 1
// Val: GoFrame
}
// Generic approach
func ExampleTRing_Val() {
r := gring.NewTRing[int](10)
r.Set(100)
fmt.Println("Val:", r.Val()) // No type assertion needed
// Output:
// Val: 100
}
Len
- Description:
Lenreturns the size of the ring (number of used elements). - Format:
Len() int
- Example:
func ExampleRing_Len() {
r1 := gring.New(10)
for i := 0; i < 5; i++ {
r1.Set(i).Next()
}
fmt.Println("Len:", r1.Len())
r2 := gring.New(10, true)
for i := 0; i < 10; i++ {
r2.Set(i).Next()
}
fmt.Println("Len:", r2.Len())
// Output:
// Len: 5
// Len: 10
}
Cap
- Description:
Capreturns the capacity of the ring (maximum number of elements). - Format:
Cap() int
- Example:
func ExampleRing_Cap() {
r1 := gring.New(10)
for i := 0; i < 5; i++ {
r1.Set(i).Next()
}
fmt.Println("Cap:", r1.Cap())
r2 := gring.New(10, true)
for i := 0; i < 10; i++ {
r2.Set(i).Next()
}
fmt.Println("Cap:", r2.Cap())
// Output:
// Cap: 10
// Cap: 10
}
Setter Methods
Set
- Description:
Setsets a value to the item at the current position. - Format:
// Traditional approach
Set(value interface{}) *Ring
// Generic approach (v2.10.0+)
Set(value T) *TRing[T]
- Example:
func ExampleRing_Set() {
r := gring.New(10)
r.Set(1)
fmt.Println("Val:", r.Val())
r.Next().Set("GoFrame")
fmt.Println("Val:", r.Val())
// Output:
// Val: 1
// Val: GoFrame
}
// Generic approach
func ExampleTRing_Set() {
r := gring.NewTRing[string](10)
r.Set("Hello")
fmt.Println("Val:", r.Val())
r.Next().Set("World")
fmt.Println("Val:", r.Val())
// Output:
// Val: Hello
// Val: World
}
Put
- Description:
Putsetsvalueto the current ring item and moves the position to the next item. - Format:
// Traditional approach
Put(value interface{}) *Ring
// Generic approach (v2.10.0+)
Put(value T) *TRing[T]
- Example:
func ExampleRing_Put() {
r := gring.New(10)
r.Put(1)
fmt.Println("Val:", r.Val())
fmt.Println("Val:", r.Prev().Val())
// Output:
// Val: <nil>
// Val: 1
}
// Generic approach
func ExampleTRing_Put() {
r := gring.NewTRing[int](10)
r.Put(100)
fmt.Println("Val:", r.Val())
fmt.Println("Val:", r.Prev().Val())
// Output:
// Val: 0
// Val: 100
}
Move Methods
Move
- Description:
Movemoves backward (n < 0) or forward (n >= 0) byn % r.Len()elements in the ring and returns that ring element.rmust not be empty. - Format:
// Traditional approach
Move(n int) *Ring
// Generic approach (v2.10.0+)
Move(n int) *TRing[T]
- Example:
func ExampleRing_Move() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
// ring at Pos 0
fmt.Println("CurVal:", r.Val())
r.Move(5)
// ring at Pos 5
fmt.Println("CurVal:", r.Val())
// Output:
// CurVal: 0
// CurVal: 5
}
Prev
- Description:
Prevreturns the previous ring element.rmust not be empty. - Format:
// Traditional approach
Prev() *Ring
// Generic approach (v2.10.0+)
Prev() *TRing[T]
- Example:
func ExampleRing_Prev() {
r := gring.New(10)
for i := 0; i < 5; i++ {
r.Set(i).Next()
}
fmt.Println("Prev:", r.Prev().Val())
fmt.Println("Prev:", r.Prev().Val())
// Output:
// Prev: 4
// Prev: 3
}
Next
- Description:
Nextreturns the next ring element.rmust not be empty. - Format:
// Traditional approach
Next() *Ring
// Generic approach (v2.10.0+)
Next() *TRing[T]
- Example:
func ExampleRing_Next() {
r := gring.New(10)
for i := 5; i > 0; i-- {
r.Set(i).Prev()
}
fmt.Println("Next:", r.Next().Val())
fmt.Println("Next:", r.Next().Val())
// Output:
// Next: 1
// Next: 2
}
Link Methods
Link
- Description:
Linkconnects ringrwith ringssuch thatr.Next()becomessand returns the original value ofr.Next().rmust not be empty.- If
randspoint to the same ring, linking them removes the elements betweenrandsfrom the ring. The removed elements form a sub-ring, and the result is a reference to that sub-ring (if no elements were removed, the result is still the original value ofr.Next(), notnil). - If
randspoint to different rings, linking them creates a single ring with the elements ofsinserted afterr. The result points to the element after the last element ofsthat was inserted.
- If
- Format:
// Traditional approach
Link(s *Ring) *Ring
// Generic approach (v2.10.0+)
Link(s *TRing[T]) *TRing[T]
- Example:
func ExampleRing_Link() {
r := gring.New(10)
for i := 0; i < 5; i++ {
r.Set(i).Next()
}
s := gring.New(10)
for i := 0; i < 10; i++ {
val := i + 5
s.Set(val).Next()
}
r.Link(s) // Link Ring s to Ring r
fmt.Println("Len:", r.Len())
fmt.Println("Cap:", r.Cap())
// Output:
// Len: 15
// Cap: 20
}
Unlink
- Description:
Unlinkremovesn % r.Len()elements from ringr, starting fromr.Next(). Ifn % r.Len() == 0,rremains unchanged. The result is the removed sub-ring.rmust not be empty. - Format:
// Traditional approach
Unlink(n int) *Ring
// Generic approach (v2.10.0+)
Unlink(n int) *TRing[T]
- Example:
func ExampleRing_Unlink() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
fmt.Println("Before Unlink, Len:", r.Len())
fmt.Println("Before Unlink, Cap:", r.Cap())
r.Unlink(7)
fmt.Println("After Unlink, Len:", r.Len())
fmt.Println("After Unlink, Cap:", r.Cap())
// Output:
// Before Unlink, Len: 10
// Before Unlink, Cap: 10
// After Unlink, Len: 3
// After Unlink, Cap: 3
}
Iterator Methods
RLockIteratorNext
- Description:
RLockIteratorNextiterates forward and locks for reading withRWMutex.RLockusing the given callback functionf. Iffreturnstrue, iteration continues; otherwise it stops whenfreturnsfalse. - Format:
// Traditional approach
RLockIteratorNext(f func(value interface{}) bool)
// Generic approach (v2.10.0+)
RLockIteratorNext(f func(value T) bool)
- Example:
func ExampleRing_RLockIteratorNext() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
r.RLockIteratorNext(func(value interface{}) bool {
if value.(int) < 5 {
fmt.Println("IteratorNext Success, Value:", value)
return true
}
return false
})
// Output:
// IteratorNext Success, Value: 0
// IteratorNext Success, Value: 1
// IteratorNext Success, Value: 2
// IteratorNext Success, Value: 3
// IteratorNext Success, Value: 4
}
// Generic approach
func ExampleTRing_RLockIteratorNext() {
r := gring.NewTRing[int](10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
// No type assertion needed
r.RLockIteratorNext(func(value int) bool {
if value < 5 {
fmt.Println("IteratorNext Success, Value:", value)
return true
}
return false
})
// Output:
// IteratorNext Success, Value: 0
// IteratorNext Success, Value: 1
// IteratorNext Success, Value: 2
// IteratorNext Success, Value: 3
// IteratorNext Success, Value: 4
}
RLockIteratorPrev
- Description:
RLockIteratorPreviterates backward and locks for reading withRWMutex.RLockusing the given callback functionf. Iffreturnstrue, iteration continues; otherwise it stops whenfreturnsfalse. - Format:
// Traditional approach
RLockIteratorPrev(f func(value interface{}) bool)
// Generic approach (v2.10.0+)
RLockIteratorPrev(f func(value T) bool)
- Example:
func ExampleRing_RLockIteratorPrev() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
// move r to pos 9
r.Prev()
r.RLockIteratorPrev(func(value interface{}) bool {
if value.(int) >= 5 {
fmt.Println("IteratorPrev Success, Value:", value)
return true
}
return false
})
// Output:
// IteratorPrev Success, Value: 9
// IteratorPrev Success, Value: 8
// IteratorPrev Success, Value: 7
// IteratorPrev Success, Value: 6
// IteratorPrev Success, Value: 5
}
Slice Methods
SliceNext
- Description:
SliceNextreturns a copy of all item values as a slice starting from the current position moving forward. - Format:
// Traditional approach
SliceNext() []interface{}
// Generic approach (v2.10.0+)
SliceNext() []T
- Example:
func ExampleRing_SliceNext() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
fmt.Println(r.SliceNext())
// Output:
// [0 1 2 3 4 5 6 7 8 9]
}
// Generic approach
func ExampleTRing_SliceNext() {
r := gring.NewTRing[string](5)
words := []string{"Go", "Frame", "is", "awesome", "!"}
for _, word := range words {
r.Put(word)
}
r.Move(-5)
fmt.Println(r.SliceNext())
// Output:
// [Go Frame is awesome !]
}
SlicePrev
- Description:
SlicePrevreturns a copy of all item values as a slice starting from the current position moving backward. - Format:
// Traditional approach
SlicePrev() []interface{}
// Generic approach (v2.10.0+)
SlicePrev() []T
- Example:
func ExampleRing_SlicePrev() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
fmt.Println(r.SlicePrev())
// Output:
// [0 9 8 7 6 5 4 3 2 1]
}
// Generic approach
func ExampleTRing_SlicePrev() {
r := gring.NewTRing[int](10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
fmt.Println(r.SlicePrev())
// Output:
// [0 9 8 7 6 5 4 3 2 1]
}