Golang - Demonstrate the use ring.Link() function Code Example

The code for Demonstrate the use ring.Link() function

package main

import (
	"container/ring"
	"fmt"
)

func main() {
	// Creating two rings, rng1 and rng2, of size 5
	rng1 := ring.New(5)
	rng2 := ring.New(5)

	// Getting the length
	lrng1 := rng1.Len()
	lrng2 := rng2.Len()

	// Initializing rng1 with "Hello"
	for i := 0; i < lrng1; i++ {
		rng1.Value = "Hello"
		rng1 = rng1.Next()
	}

	// Initializing rng2 with "World"
	for j := 0; j < lrng2; j++ {
		rng2.Value = "World"
		rng2 = rng2.Next()
	}

	// Linking ring rng1 and ring rng2
	rng3 := rng1.Link(rng2)

	// Iterating through the combined
	// ring and print its contents
	rng3.Do(func(p interface{}) {
		fmt.Println(p.(string))
	})
}


/*
Output:
Hello
Hello
Hello
Hello
Hello
World
World
World
World
World
*/
Code by IncludeHelp, on February 28, 2023 16:29

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.