Do() function of ring package is used to call function f on each element of the ring in forward order in go.
Do() function prototype of ring package:
func (r *Ring) Do(f func(any)) Here, type any = interface{}
Return type in Do() function of ring package:
Do() does not returns anything. It performs operations of ring elements in function defined by user.
Example of Do() function in ring package:
Code 1:
package main
import (
"container/ring"
"fmt"
)
func main() {
// Create a new ring of size 5
r := ring.New(5)
// 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
r.Do(func(p any) {
fmt.Println(p.(int))
})
}
Output:
0 1 2 3 4
Code 2:
package main
import (
"container/ring"
"fmt"
)
// Square numbers of ring elements
func square_numbers(n any) {
ele := n.(int)
fmt.Println(ele * ele)
}
func main() {
// Create a new ring of size 6
r := ring.New(6)
// 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 elements
fmt.Println("Ring elements are: ")
r.Do(square_numbers)
}Output:
Ring elements are: 0 1 4 9 16 25
To learn more about golang, Please refer given below link:
References: