我正在查看 Go 的堆包 ( https://golang.org/pkg/container/heap/ ) Priority Queue 示例并遇到了这个:
type PriorityQueue []*Item
...
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}
当我开始玩弄这段代码以确保我理解它时,我尝试了:
item := *pq[0] // error
这使您键入 *[]T 不支持索引。但如果你这样做:
item := (*pq)[0] // all is well
这是类型断言吧?希望有人能解释这里发生了什么。
这是一些快速显示的代码:https : //play.golang.org/p/uAzYASrm_Q
牧羊人nacy
相关分类