golang 中sort包sort.search()使用教程

豆瓣奶茶 · · 4074 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

使用sort包中Sort()函数的时候
cannot use c.ring (type []uint32) as type sort.Interface in argument to sort.Sort:
[]uint32 does not implement sort.Interface (missing Len method)

cannot use c.ring (type []uint32) as type sort.Interface in argument to sort.Sort:
        []uint32 does not implement sort.Interface (missing Len method)

原因是sort.Sort()的定义如下

func Sort(data Interface)

type Interface interface {
    // Len方法返回集合中的元素个数
    Len() [int](https://studygolang.com/static/pkgdoc/pkg/builtin.htm#int)
    // Less方法报告索引i的元素是否比索引j的元素小
    Less(i, j [int](https://studygolang.com/static/pkgdoc/pkg/builtin.htm#int)) [bool](https://studygolang.com/static/pkgdoc/pkg/builtin.htm#bool)
    // Swap方法交换索引i和j的两个元素
    Swap(i, j [int](https://studygolang.com/static/pkgdoc/pkg/builtin.htm#int))
}

一个满足sort.Interface接口的(集合)类型可以被本包的函数进行排序。方法要求集合中的元素可以被整数索引

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:豆瓣奶茶

查看原文:golang 中sort包sort.search()使用教程

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

4074 次点击  
加入收藏 微博
2 回复  |  直到 2021-09-13 10:37:24
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传