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 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题