Golang container/ring.Ring.Move function example
package container/ring
Move moves the given input (n) elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. The ring that needs to be moved, must not be empty.
Golang container/ring.Ring.Move function usage example
package main
import (
"container/ring"
"fmt"
)
func main() {
r := ring.New(10)
// populate our ring
for i := 0; i < 10; i++ {
r.Value = i
r = r.Next()
}
f := func(v interface{}) {
fmt.Printf("%d ", v)
}
// Values in r before move
fmt.Println("Values in r BEFORE move")
r.Do(f)
fmt.Println()
moved := r.Move(2)
// Values in r AFTER move
fmt.Println("Values in r AFTER moving 2 positive")
moved.Do(f)
fmt.Println()
movedagain := moved.Move(-2)
// Values in r AFTER move
fmt.Println("Values in r AFTER moving 2 negative")
movedagain.Do(f)
fmt.Println()
}
Output :
Values in r BEFORE move
0 1 2 3 4 5 6 7 8 9
Values in r AFTER moving 2 positive
2 3 4 5 6 7 8 9 0 1
Values in r AFTER moving 2 negative
0 1 2 3 4 5 6 7 8 9
Reference :
Advertisement
Something interesting
Tutorials
+6k Golang : Compound interest over time example
+16.8k Golang : Get own process identifier
+4.3k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+12.6k Golang : flag provided but not defined error
+37.5k Upload multiple files with Go
+19.9k Golang : Count JSON objects and convert to slice/array
+5.9k Golang : Use NLP to get sentences for each paragraph example
+7k Golang : Takes a plural word and makes it singular
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+18.4k Golang : Logging with logrus
+18.8k Golang : How to make function callback or pass value from function as parameter?
+9.6k Golang : How to generate Code 39 barcode?