Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ring.Link() Function in Golang With Examples

  • Last Updated : 17 May, 2020

ring.Link() function in Golang is used to connect two ring r and ring s ring.next() function connect last node of ring r to first node of ring s .so that for r.next() must not be empty. Otherwise, it will throw an error. It works like a circular linked list.

Syntax:

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

It does not return anything.

Example 1:




// Golang program to illustrate
// the ring.Link() function
package main
  
import (
    "container/ring"
    "fmt"
)
  
// Main function
func main() {
  
    // Create two rings, a and b, of size 2
    a := ring.New(4)
    b := ring.New(4)
  
    // Get the length of the ring
    m := a.Len()
    n := b.Len()
  
    // Initialize a with 0s
    for j := 0; j < m; j++ {
        a.Value = 0
        a = a.Next()
    }
  
    // Initialize b with 1s
    for i := 0; i < n; i++ {
        b.Value = 1
        b = b.Next()
    }
  
    // Link ring a and ring b
    ab := a.Link(b)
  
    ab.Do(func(p interface{}) {
        fmt.Println(p.(int))
    })
  
}

Output:

0
0
0
0
1
1
1
1

Example 2:




// Golang program to illustrate
// the ring.Link() function
package main
  
import (
    "container/ring"
    "fmt"
)
  
// Main function
func main() {
  
    // Create two rings, r and s, of size 2
    r := ring.New(2)
    s := ring.New(2)
  
    // Get the length of the ring
    lr := r.Len()
    ls := s.Len()
  
    // Initialize r with "GFG"
    for i := 0; i < lr; i++ {
        r.Value = "GFG"
        r = r.Next()
    }
  
    // Initialize s with "COURSE"
    for j := 0; j < ls; j++ {
        s.Value = "COURSE"
        s = s.Next()
    }
  
    // Link ring r and ring s
    rs := r.Link(s)
  
    // Iterate through the combined
    // ring and print its contents
    rs.Do(func(p interface{}) {
        fmt.Println(p.(string))
    })
}

Output:

GFG
GFG
COURSE
COURSE


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!