Go語言列表List獲取元素的4種方式
Golang的列表元素的獲取可以使用內(nèi)置的 Front 函數(shù)獲取頭結(jié)點(diǎn),使用 Back 函數(shù)獲取尾結(jié)點(diǎn),使用 Prev 獲取前一個(gè)結(jié)點(diǎn),使用 Next 獲取下一個(gè)結(jié)點(diǎn)。
1、獲取列表頭結(jié)點(diǎn)
Front() *Element
package main import ( "container/list" "fmt" ) func main() { fmt.Println("嗨客網(wǎng)(www.haicoder.net)") //使用列表內(nèi)置的 Front() 函數(shù),獲取列表的頭結(jié)點(diǎn) listHaiCoder := list.New() listHaiCoder.PushFront("Hello") listHaiCoder.PushFront("HaiCoder") listHaiCoder.PushFront("嗨客網(wǎng)") element := listHaiCoder.Front() fmt.Println("Front =", element.Value) }
使用列表內(nèi)置的 Front() 函數(shù),獲取列表的頭結(jié)點(diǎn)。
2、獲取列表尾結(jié)點(diǎn)
Back () *Element
package main import ( "container/list" "fmt" ) func main() { fmt.Println("嗨客網(wǎng)(www.haicoder.net)") //使用列表內(nèi)置的 Back() 函數(shù),獲取列表的尾結(jié)點(diǎn) listHaiCoder := list.New() listHaiCoder.PushFront("Hello") listHaiCoder.PushFront("HaiCoder") listHaiCoder.PushFront("嗨客網(wǎng)") element := listHaiCoder.Back() fmt.Println("Back =", element.Value) }
使用列表內(nèi)置的 Back() 函數(shù),獲取列表的尾結(jié)點(diǎn)。
3、獲取上一個(gè)結(jié)點(diǎn)
Prev() *Element
package main import ( "container/list" "fmt" ) func main() { fmt.Println("嗨客網(wǎng)(www.haicoder.net)") //使用列表內(nèi)置的 Prev() 函數(shù),獲取列表的上一個(gè)結(jié)點(diǎn) listHaiCoder := list.New() listHaiCoder.PushFront("Hello") element := listHaiCoder.PushFront("HaiCoder") listHaiCoder.PushFront("嗨客網(wǎng)") preElement := element.Prev() fmt.Println("preElement =", preElement.Value) }
使用列表內(nèi)置的 Prev() 函數(shù),獲取列表的上一個(gè)結(jié)點(diǎn)。
4、獲取下一個(gè)結(jié)點(diǎn)
Next() *Element
package main import ( "container/list" "fmt" ) func main() { fmt.Println("嗨客網(wǎng)(www.haicoder.net)") //使用列表內(nèi)置的 Next() 函數(shù),獲取列表的下一個(gè)結(jié)點(diǎn) listHaiCoder := list.New() listHaiCoder.PushFront("Hello") element := listHaiCoder.PushFront("HaiCoder") listHaiCoder.PushFront("嗨客網(wǎng)") nextElement := element.Next() fmt.Println("nextElement =", nextElement.Value) }
使用列表內(nèi)置的 Next() 函數(shù),獲取列表的下一個(gè)結(jié)點(diǎn)。
到此這篇關(guān)于Go語言列表List獲取元素的4種方式的文章就介紹到這了,更多相關(guān)Go 列表List獲取元素內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
go語言題解LeetCode989數(shù)組形式的整數(shù)加法
這篇文章主要為大家介紹了go語言題解LeetCode989數(shù)組形式的整數(shù)加法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12go語言實(shí)現(xiàn)同步操作項(xiàng)目示例
本文主要介紹了go語言實(shí)現(xiàn)同步操作項(xiàng)目示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Go語言從單體服務(wù)到微服務(wù)設(shè)計(jì)方案詳解
這篇文章主要為大家介紹了Go語言從單體服務(wù)到微服務(wù)設(shè)計(jì)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Makefile構(gòu)建Golang項(xiàng)目示例詳解
這篇文章主要為大家介紹了Makefile構(gòu)建Golang項(xiàng)目的過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07gin通過go build -tags實(shí)現(xiàn)json包切換及庫分析
這篇文章主要為大家介紹了gin通過go build -tags實(shí)現(xiàn)json包切換及庫分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09golang?四則運(yùn)算計(jì)算器yacc歸約手寫實(shí)現(xiàn)
這篇文章主要為大家介紹了golang?四則運(yùn)算?計(jì)算器?yacc?歸約的手寫實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07