I was looking at golang's heap package's (https://golang.org/pkg/container/heap/) Priority Queue example and came across this:
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
}
When I started playing around with this code to make sure I understood it, I tried:
item := *pq[0] // error
This gives you type *[]T does not support indexing. But if you do:
item := (*pq)[0] // all is well
This is type assertion right? Was hoping someone could explain what is going on here.
Here is some code to quickly show this: https://play.golang.org/p/uAzYASrm_Q