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条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用