Golang container/ring.Ring.Prev function example

package container/ring

Prev returns the previous ring element. The ring that is to be "Prev"ed must not be empty.

Golang container/ring.Ring.Prev function usage example

 package main

 import (
 "container/ring"
 "fmt"
 )

 func main() {

  r := ring.New(10)

  // populate our ring
  for i := 10; i > 0; i-- {
 r.Value = i
 r = r.Prev() // Prev element to populate
  }

  fmt.Printf("%d ", r.Value)
  reverse := r.Prev()
  for ; reverse != r; reverse = reverse.Prev() {
 fmt.Printf("%d ", reverse.Value)
  }

  fmt.Println()

 }

Output :

10 9 8 7 6 5 4 3 2 1

Reference :

http://golang.org/pkg/container/ring/#Ring.Prev

Advertisement