[英]Go Golang : Type assertion on customized type
http://play.golang.org/p/icQO_bAZNE
我正在使用堆練習排序,但是
prog.go:85: type bucket is not an expression
prog.go:105: cannot use heap.Pop(bucket[i].([]IntArr)) (type interface {}) as type int in assignment: need type assertion
[process exited with non-zero status]
我收到了這些錯誤,無法弄清楚如何正確地輸入斷言
問題出在以下幾行:
heap.Push(bucket[x].([]IntArr), elem)
arr[index] = heap.Pop(bucket[i].([]IntArr))
因為我想使用堆結構以便從每個存儲桶中提取值
每個存儲桶都是[]IntArr
IntArr
是[]int
,如下所示
type IntArr []int
type bucket [10]IntArr
周末我一直在嘗試許多方法,無法弄清楚,對此我非常感謝。
要使用堆包,您應該為您的類型實現heap.Interface(在這種情況下,對於IntArr類型)。 您可以在此處找到示例: http : //golang.org/pkg/container/heap/#pkg-examples
然后你可以做類似的事情
heap.Push(bucket[x], elem)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.