亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

字符串列表 - golang

字符串列表 - golang

Go
青春有我 2022-01-04 21:11:36
我正在嘗試在 golang 中制作一個字符串列表。我正在查找包容器/列表,但我不知道如何放入字符串。我嘗試了幾次,但結果為 0。我應該使用其他東西而不是列表嗎?提前致謝。編輯:不知道你為什么用否定票來評價這個問題......
查看完整描述

2 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

修改您鏈接的確切示例,并將整數更改為字符串對我有用:


package main


import (

    "container/list"

    "fmt"

)


func main() {

    // Create a new list and put some numbers in it.

    l := list.New()

    e4 := l.PushBack("4")

    e1 := l.PushFront("1")

    l.InsertBefore("3", e4)

    l.InsertAfter("2", e1)


    // Iterate through list and print its contents.

    for e := l.Front(); e != nil; e = e.Next() {

        fmt.Println(e.Value)

    }

}


查看完整回答
反對 回復 2022-01-04
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

如果您查看鏈接的包的源代碼,該List類型似乎包含一個Elements列表。望著Element你會看到它有一個出口領域被稱為Value是一種interface{}類型,這意味著它可能是從字面上任何東西:string,int,float64,io.Reader,等。


要回答您的第二個問題,您會看到List有一個名為Remove(e *Element). 你可以這樣使用它:


fmt.Println(l.Len()) // prints: 4


// Iterate through list and print its contents.

for e := l.Front(); e != nil; e = e.Next() {

    if e.Value == "4" {

        l.Remove(e) // remove "4"

    } else {

        fmt.Println(e.Value)

    }

}


fmt.Println(l.Len()) // prints: 3

總的來說,Golang 文檔通常非??煽?,因此您應該始終先查看那里。


https://golang.org/pkg/container/list/#Element


查看完整回答
反對 回復 2022-01-04
  • 2 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號