SoFunction
Updated on 2025-03-05

4 ways to get elements in Go language list

Golang's list elements can use the built-in Front function to get the header node, use the Back function to get the tail node, use Prev to get the previous node, and use Next to get the next node.

1. Get the list header node

Front() *Element

package main
import (
    "container/list"
    "fmt"
)
func main() {
    ("Hike.com()")
    //Use the built-in Front() function of the list to get the header node of the list    listHaiCoder := ()
    ("Hello")
    ("HaiCoder")
    ("Hike.com")
    element := ()
    ("Front =", )
}

Use the list's built-in Front() function to get the list's header node.

2. Get the end of the list

Back () *Element

package main
import (
    "container/list"
    "fmt"
)
func main() {
    ("Hike.com()")
    //Use the built-in Back() function of the list to get the end of the list    listHaiCoder := ()
    ("Hello")
    ("HaiCoder")
    ("Hike.com")
    element := ()
    ("Back =", )
}

Use the built-in Back() function of the list to get the end of the list.

3. Get the previous node

Prev() *Element

package main
import (
    "container/list"
    "fmt"
)
func main() {
    ("Hike.com()")
    //Use the list's built-in Prev() function to get the previous node of the list    listHaiCoder := ()
    ("Hello")
    element := ("HaiCoder")
    ("Hike.com")
    preElement := ()
    ("preElement =", )
}

Use the list's built-in Prev() function to get the previous node of the list.

4. Get the next node

Next() *Element

package main
import (
    "container/list"
    "fmt"
)
func main() {
    ("Hike.com()")
    //Use the next() function built in the list to get the next node of the list    listHaiCoder := ()
    ("Hello")
    element := ("HaiCoder")
    ("Hike.com")
    nextElement := ()
    ("nextElement =", )
}

Use the next() function built in the list to get the next node of the list.

This is the end of this article about 4 ways to obtain elements in Go language list List. For more related Go list List content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!