指向切片和数组的指针
Pointer to slice and array
我在查看 Go 的堆包的 (https://golang.org/pkg/container/heap/) 优先级队列示例时遇到了这个:
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
对您有用的不是类型断言,而是操作顺序。
问题的根源在于索引先于指针的取消引用。一旦你在指针取消引用周围加上大括号,它就会运行良好,因为索引应用于现在取消引用的 PriorityQueue
实例。
您不需要对数组指针执行此操作,因为它们会自动取消引用 - 索引数组和切片之间的细微差别在此处解释:The Go Programming Language Specification - Index expressions
For a
of array type A
:
- a constant index must be in range
- if
x
is out of range at run time, a run-time panic occurs
a[x]
is the array element at index x
and the type of a[x]
is the element type of A
For a
of pointer to array type:
a[x]
is shorthand for (*a)[x]
For a
of slice type S
:
- if
x
is out of range at run time, a run-time panic occurs
a[x]
is the slice element at index x
and the type of a[x]
is the element type of S
我在查看 Go 的堆包的 (https://golang.org/pkg/container/heap/) 优先级队列示例时遇到了这个:
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
对您有用的不是类型断言,而是操作顺序。
问题的根源在于索引先于指针的取消引用。一旦你在指针取消引用周围加上大括号,它就会运行良好,因为索引应用于现在取消引用的 PriorityQueue
实例。
您不需要对数组指针执行此操作,因为它们会自动取消引用 - 索引数组和切片之间的细微差别在此处解释:The Go Programming Language Specification - Index expressions
For
a
of array typeA
:
- a constant index must be in range
- if
x
is out of range at run time, a run-time panic occursa[x]
is the array element at indexx
and the type ofa[x]
is the element type ofA
For
a
of pointer to array type:
a[x]
is shorthand for(*a)[x]
For
a
of slice typeS
:
- if
x
is out of range at run time, a run-time panic occursa[x]
is the slice element at indexx
and the type ofa[x]
is the element type ofS