[英]Go Golang : Type assertion on customized type
http://play.golang.org/p/icQO_bAZNE http://play.golang.org/p/icQO_bAZNE
I am practicing sort using heap but 我正在使用堆练习排序,但是
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]
I am getting those errors and can't figure out how to type assert properly 我收到了这些错误,无法弄清楚如何正确地输入断言
The problem is from the lines: 问题出在以下几行:
heap.Push(bucket[x].([]IntArr), elem)
arr[index] = heap.Pop(bucket[i].([]IntArr))
Because I want to use heap structure in order to extract values from each bucket 因为我想使用堆结构以便从每个存储桶中提取值
And each bucket is []IntArr
每个存储桶都是
[]IntArr
And IntArr
is []int
like the following IntArr
是[]int
,如下所示
type IntArr []int
type bucket [10]IntArr
I have been trying many ways over the weekend and can't figure out, I greatly appreciate it. 周末我一直在尝试许多方法,无法弄清楚,对此我非常感谢。
For an expression x of interface type and a type T, the primary expression
对于具有接口类型和类型T的表达式x,主要表达式
x.(T)
asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.
断言x不为nil,并且x中存储的值的类型为T。符号x。(T)称为类型断言。
bucket[x]
is not an expression of an interface type, see more here . bucket[x]
不是接口类型的表达式,请参见此处 。
To use heap package you should implement heap.Interface for your type (in this case, for your IntArr type). 要使用堆包,您应该为您的类型实现heap.Interface(在这种情况下,对于IntArr类型)。 You can find example here: http://golang.org/pkg/container/heap/#pkg-examples
您可以在此处找到示例: http : //golang.org/pkg/container/heap/#pkg-examples
Then you can do things like 然后你可以做类似的事情
heap.Push(bucket[x], elem)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.