3 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
為了保持簡(jiǎn)單,我將使用map。地圖非??焖?、高效且內(nèi)置。
package main
import "fmt"
func main() {
// Make our collection of integers
xs := make(map[int]bool)
// Add some things to the collection
xs[1] = true
xs[2] = true
xs[3] = true
// Find them
if xs[2] {
fmt.Println("Found 2")
} else {
fmt.Println("Didn't Find 2")
}
if xs[8] {
fmt.Println("Found 8")
} else {
fmt.Println("Didn't Find 8")
}
// Delete them
delete(xs, 2)
// List them
for x := range xs {
fmt.Println("Contents", x)
}
}
其中產(chǎn)生
發(fā)現(xiàn) 2
沒(méi)找到 8
內(nèi)容 3
內(nèi)容 1
這種解決方案的唯一缺點(diǎn)可能是整數(shù)沒(méi)有按任何特定順序保存,這對(duì)您的應(yīng)用程序可能重要也可能不重要。
- 3 回答
- 0 關(guān)注
- 231 瀏覽
添加回答
舉報(bào)