3 回答

TA贡献1821条经验 获得超6个赞
为了保持简单,我将使用map。地图非常快速、高效且内置。
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)
}
}
其中产生
发现 2
没找到 8
内容 3
内容 1
这种解决方案的唯一缺点可能是整数没有按任何特定顺序保存,这对您的应用程序可能重要也可能不重要。
- 3 回答
- 0 关注
- 211 浏览
添加回答
举报