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:
package main
import (
"container/ring"
"fmt"
)
func main() {
a := ring.New(4)
b := ring.New(4)
m := a.Len()
n := b.Len()
for j := 0; j < m; j++ {
a.Value = 0
a = a.Next()
}
for i := 0; i < n; i++ {
b.Value = 1
b = b.Next()
}
ab := a.Link(b)
ab.Do(func(p interface{}) {
fmt.Println(p.( int ))
})
}
|
Output:
0
0
0
0
1
1
1
1
Example 2:
package main
import (
"container/ring"
"fmt"
)
func main() {
r := ring.New(2)
s := ring.New(2)
lr := r.Len()
ls := s.Len()
for i := 0; i < lr; i++ {
r.Value = "GFG"
r = r.Next()
}
for j := 0; j < ls; j++ {
s.Value = "COURSE"
s = s.Next()
}
rs := r.Link(s)
rs.Do(func(p interface{}) {
fmt.Println(p.(string))
})
}
|
Output:
GFG
GFG
COURSE
COURSE
Share your thoughts in the comments
Please Login to comment...