gpt4 book ai didi

arrays - 指向 slice 和数组的指针

转载 作者:IT王子 更新时间:2023-10-29 00:39:08 25 4
gpt4 key购买 nike

我在查看 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 实例。

您不需要对数组指针执行此操作,因为它们会自动取消引用 - 索引数组和 slice 之间的细微差别在此处解释: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

关于arrays - 指向 slice 和数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36242018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com