Golang container/ring.Ring.Unlink() function example

package container/ring

Unlink removes n (1st parameter) elements from the ring r, starting at r.Next(). If n is not found, the ring r remains unchanged. The result is the removed subring. Before unlink operation on the ring r. The ring r must not be empty.

Golang container/ring.Ring.Unlink() 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()

  r.Unlink(6)


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

  fmt.Println()

 }

Output :

Before Unlink :

10 9 8 7 6 5 4 3 2 1

After Unlink :

10 9 8 7

Reference :

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

Advertisement