drhe80011 2013-12-23 23:31
浏览 557
已采纳

Go Golang:自定义类型上的类型断言

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

And IntArr is []int like the following

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.

  • 写回答

2条回答 默认 最新

  • dream890110 2013-12-24 00:00
    关注

    To use heap package you should implement heap.Interface for your type (in this case, for your IntArr type). You can find example here: http://golang.org/pkg/container/heap/#pkg-examples

    Then you can do things like

    heap.Push(bucket[x], elem)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料