Get the App
SLTechnology News&Howtos  ›  Development  › 

Go language list what are the ways in which List can get elements

Shulou Source: shulou.com Published: 2022-05-31 20:37:57 10月25日 Update

This article mainly introduces the Go language list to get elements of the way there are related knowledge, content detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone read this Go language list to get elements of the way there are articles will be harvested, let's take a look at it.

Golang's list elements can be obtained by using the built-in Front function to obtain the head node, using the Back function to obtain the tail node, using Prev to obtain the previous node, and using Next to obtain the next node.

1. Obtain column header node

Front() *Element

package mainimport ( "container/list" "fmt")func main() { fmt.Println("www.haicoder.net") //Use the built-in Front() function to get the head node of the list listHaiCoder := list.New() listHaiCoder.PushFront("Hello") listHaiCoder.PushFront("HaiCoder") listHaiCoder.PushFront("Haike") element := listHaiCoder.Front() fmt.Println("Front =", element.Value)}

Use the built-in Front() function to get the head node of the list.

2. Get the list tail node

Back () *Element

package mainimport ( "container/list" "fmt")func main() { fmt.Println("www.haicoder.net") //Use the built-in Back() function to get the tail node of the list listHaiCoder := list.New() listHaiCoder.PushFront("Hello") listHaiCoder.PushFront("HaiCoder") listHaiCoder.PushFront("Haike") element := listHaiCoder.Back() fmt.Println("Back =", element.Value)}

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

3. Get the previous node

Prev() *Element

package mainimport ( "container/list" "fmt")func main() { fmt.Println("www.haicoder.net") //Use the built-in Prev() function to get the previous node in the list listHaiCoder := list.New() listHaiCoder.PushFront("Hello") element := listHaiCoder.PushFront("HaiCoder") listHaiCoder.PushFront("Haike") preElement := element.Prev() fmt.Println("preElement =", preElement.Value)}

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

4. Get the next node

Next() *Element

package mainimport ( "container/list" "fmt")func main() { fmt.Println("www.haicoder.net") //Use the Next() function built into the list to get the next node in the list listHaiCoder := list.New() listHaiCoder.PushFront("Hello") element := listHaiCoder.PushFront("HaiCoder") listHaiCoder.PushFront("Haike") nextElement := element.Next() fmt.Println("nextElement =", nextElement.Value)}

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

The content of this article on "What are the ways to get elements from Go Language List" is introduced here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "Go Language List to get elements." If you still want to learn more knowledge, please pay attention to the industry information channel.

Tags: Nodes functions elements methods language knowledge content articles value easy to operate articles easy to understand more look industry information channels channels learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker Huawei MySQL Redmi