Menu Close

Go – Link() function of ring package in go

Link() function of ring package is used to connect two rings elements in go golang where ring must not be empty.

Link() function links ring “r” with ring “s” such that r.Next() becomes s and returns the original value for r.Next().

Syntax:

func (r *Ring) Link(s *Ring) *Ring

Return type in link() function of ring package:

 Link() returns head pointer of merged list.

Explanation:

  • If the ring r and s point to the difference ring, linking them will create a single ring with the elements of s inserted after r.
  • If the ring r and s point to the same ring, linking them will remove the elements between r and s from the ring.

Example:

ring r := 1 -> 2 -> 3 -> 4 , here r is pointing to 1
ring s:= 5 -> 6 -> 7 -> 8 , here s is pointing to 5

If you do link between two rings i.e r and s then r.Next() i.e 1 -> , will point to s i.e 5.

So, After linking rings:

rs := r.Link(s)

Final list will be : 2 -> 3 -> 4 -> 1 -> 5 -> 6 -> 7 -> 8

*** ring r and s point to the difference ring

package main


import (

	"container/ring"

	"fmt"

)


func main() {
    // ring r and s point to the difference ring
	// Create a two rings of size 5
	r := ring.New(4)

	s := ring.New(4)


	// Get the length of the ring
	n := r.Len()


	// Initialize the ring r with some integer values
	for i := 1; i <= n; i++ {

		r.Value = i

		r = r.Next()

	}


	// Initialize the ring 2 with some integer values
	for i := 1; i <= n; i++ {

		s.Value = i + 4

		s = s.Next()

	}


	// Iterate through the ring elements
	fmt.Println("Ring r elements are: ")

	r.Do(func(p any) {

		fmt.Println(p.(int))

	})


	fmt.Println("\nRing s elements are: ")

	s.Do(func(p any) {

		fmt.Println(p.(int))

	})


	// Link ring r and ring s
	rs := r.Link(s)


	// Iterate through the combined ring and print its contents
	fmt.Println("\nRing elements after combining: ")

	rs.Do(func(p any) {

		fmt.Println(p.(int))

	})

}

Output:

Ring r elements are:
1
2
3
4

Ring s elements are:
5
6
7
8

Ring elements after combining:
2
3
4
1
5
6
7
8

*** ring r and s point to the same ring

package main


import (

	"container/ring"

	"fmt"

)


func main() {
	// Create a two rings of size 5
	r := ring.New(4)

	s := r


	// Get the length of the ring
	n := r.Len()


	// Initialize the ring r with some integer values
	for i := 1; i <= n; i++ {

		r.Value = i

		r = r.Next()

	}


	// Iterate through the ring elements
	fmt.Println("Ring r elements are: ")
	r.Do(func(p any) {

		fmt.Println(p.(int))

	})


	fmt.Println("\nRing s elements are: ")

	s.Do(func(p any) {

		fmt.Println(p.(int))

	})


	// Link ring r and ring s
	rs := r.Link(s)


	// Iterate through the combined ring and print its contents
	fmt.Println("\nRing elements after combining: ")

	rs.Do(func(p any) {

		fmt.Println(p.(int))

	})

}

Output:

Ring r elements are:
1
2
3
4

Ring s elements are:
1
2
3
4

Ring elements after combining:
2
3
4

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-ring-package-in-go/

References:

https://golang.org/doc/

Posted in golang, packages, ring

Leave a Reply

Your email address will not be published. Required fields are marked *