ds42774 2016-05-28 01:06
浏览 29

Go-如何复制界面切片?

I'm trying to implement a simple merge sort using an auxiliary array. I have type byString []string that implements the Less, Swap, and Len methods. It's basically following Go's sort package's interface.

However, I'm having some difficulty choosing the best route to copy the byString slice to a temporary array.

Please help me break out of Java's polymorphism world to make it work with Go.

func merge(data Interface, lo, mid, hi int) {
  i, j := lo, mid+1

  // How do I copy data's elements to a new slice called aux?
}
  • 写回答

1条回答 默认 最新

  • doude1917 2016-05-28 01:44
    关注

    Use the built-in copy function, you just have to declare the new slice as an interface type:

    type Interface []string
    
    func merge(data Interface, lo, mid, hi int) {
        i, j := lo, mid+1
        var aux Interface = make([]string, len(data), len(data))
        copy(aux, data)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教