Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
container/ring
package
communitycreator

Container/ring package in Golang

Chinonso Okike

The container package contains a ring package which helps with the implementation of a circular linked list. A circular linked list is a variation of a linked list in which the first element is connected to the last element and the last element is connected to the first element. Therefore, it goes in a circular path. This means that the next node on the circular list is not null.

The structure of the ring list is comprised of:

type Ring struct{
 Next, Prev *Ring
 Value interface{}
}

Check the code snippet below to see how to import the package:

package main

import "container/ring"

Initialization of rings and the length of a ring

The initialization of a ring is done using the func New(n int) *Ring method which takes an integer as a parameter and returns a pointer to a ring.

Initializing a ring tells the length. When writing programs, it is helpful to be able to get the length of the ring you are using. The length is gotten using the func (r *Ring) Len() int method which takes in no parameter and returns an integer. The integer is the number of elements contained in the ring.

package main 

import (
  "container/ring"
  "fmt"
)

func main (){
  r := ring.New(5) //initialising

  l := r.Len() //length of ring

Populating a ring

In order to add values to a ring that is looping through the length, you must specify the value of each node by updating it to the next node.

for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

Printing values of a ring

The values in a ring can be printed by using the func (r *Ring) Do(f func(interface{})) method in the container/ring package of the standard library. This method takes in a function as its parameter. The function takes an interface to print the value of the ring.

r.Do(func(p interface{}) {
		fmt.Println(p)
	})

Moving the ring

This means changing the circular linked list with which the ring has been populated from a particular position. The method for doing this is the func (r *Ring) Move(n int) *Ring which returns a pointer to a ring.

package main

import (
  "container/ring"
  "fmt"
)

func main (){
  r := ring.New(5)
	// Populating the ring with some values
	for i := 5; i < 10; i++ {
    if i == 5{
      r.Value = "a"
	  r = r.Next() // moving to the next node in the circle
      continue
    }
		r.Value = i
		r = r.Next()
	}
	// Iterate through the ring and print its contents
	r.Do(func(p interface{}) {
		fmt.Println(p)
	})
	r = r.Move(3)
	fmt.Println("========")
	r.Do(func(p interface{}) {
		fmt.Println(p)
	})

}
Populating and Print the value in a ring

How to link rings and unlike rings

Linking is joining two different rings to create a new ring. If two rings point to the same ring, joining them removes the elements between r and s from the ring.

//initialization 
  r := ring.New(2)
  s := ring.New(2)

  //Linking
	ls := r.Link(s)

Unlink deletes n % r.Len() elements from starting at r.Next()of the ring. The ring must not be empty.

//Unliking
ls.Unlink(3) 
package main

import (
  "container/ring"
  "fmt"
)

func main (){
	//initialization 
  r := ring.New(2)
  s := ring.New(2)
  //populating 
	for i := 0; i < r.Len(); i++ {
		r.Value = i
		r = r.Next()
		s.Value = i+1
		s = s.Next()
	}

	//Linking
	ls := r.Link(s)

	fmt.Println("linking r and s")
	ls.Do(func(p interface{}) {
			fmt.Println(p)
		})

	fmt.Println("Unlinking ls at point 3")
	ls.Unlink(3) //Unliking
	ls.Do(func(p interface{}) {
			fmt.Println(p)
		})

}
Linking and unlinking a ring.

In this shot you have learned how to initialize, populate, print, link, unlink, and move a circular link list in Go using the container/ring package. Have fun playing with rings.

RELATED TAGS

golang
container/ring
package
communitycreator
RELATED COURSES

View all Courses

Keep Exploring