Home ยป How to Get the Next Element in the List Using Golang Next()

How to Get the Next Element in the List Using Golang Next()

the list package in Golang has the Next() function to get the next element in the list. Golang Next() function returns the next element in the list or nil.

Golang list package implements a doubly linked list and has methods to move Back, front, and Next to get elements from the list in go.

To use the Golang Next function, you must import the list package in the Go program.

Golang Next() Function Syntax

func (e *Element) Next() *Element

It returns the next list element or nil.

In this tutorial, we will discuss how to use the Golang Next() function to get the next list element.

How to Get the Next List Element in Golang

To get the next element in the list, import the list package in the go program and use the Next() function.

package main

import (
	"container/list"
	"fmt"
)

func main() {

	// Create a list
	mylist := list.New()

	// Insert elements in list

	mylist.PushBack(10)
	mylist.PushBack(20)
	mylist.PushBack(30)
	mylist.PushBack(40)

	// Get the head element of the list
	item := mylist.Front()

	// Print the head of the list
	fmt.Println("First Element:", item.Value)

	// Move to the next element and get the next element of the list
	item = item.Next()

	// Print the next element of the list
	fmt.Println("Next Element in list:", item.Value)

}

In the above Golang program, we import the list package to use the Next() function and the fmt package to use the Println() function.

To use the Next function in Golang, follow the below steps:

  1. Create a new list, use code mylist := list.New()
  2. It creates an object mylist
  3. Use the PushBack() method to add numbers to a list
  4. To use the list, get the head of the list using the code mylist.Front()
  5. Use the Next() function to move to the next element in a list and get the next element.
  6. Use Println() to print the next element in the list.

The output of the above golang program to get the next element in the list using the Golang Next() function is:

First Element: 10
Next Element in list: 20

How to Use Golang Next () in For loop to Iterate Over List

We can use the Golang Next() function in the for loop to iterate over the list item.

package main

import (
	"container/list"
	"fmt"
)

func main() {

	// Create a list
	mylist := list.New()

	// Insert elements in list

	mylist.PushBack(10)
	mylist.PushBack(20)
	mylist.PushBack(30)
	mylist.PushBack(40)

	for ele := mylist.Front(); ele != nil; ele = ele.Next() {

		fmt.Println("Element in list:", ele.Value)
	}

}

In the above Golang program, we import the fmt and list package to use the Next() function in Go.

To use the Next() function in the for loop, follow the below steps:

  1. Create a list using the list.New(), creates a list object.
  2. use the PushBack() method to add numbers to the list.
  3. In the for loop, get the front element of the list in ele
  4. Iterate the loop till ele is not equal to nil
  5. Use ele.Next() to move to the next list element.
  6. Use the Println() to print the element in the list

The output of the above program using the golang next function in the for loop is:

Element in list: 10
Element in list: 20
Element in list: 30
Element in list: 40

Cool Tip: How to split string by regex in Golang!

Conclusion

I hope the above article on how to use the Golang Next() function to return the next element in the list is helpful to you.

You can find more topics about the Golang tutorials on the GolangSpot Home page.

Leave a Comment