登录
首页 >  Golang >  Go问答

空接口切片排序指南:如何对具有相同字段的空接口切片进行排序?

来源:stackoverflow

时间:2024-02-26 16:18:24 170浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《空接口切片排序指南:如何对具有相同字段的空接口切片进行排序?》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我有多个结构:

type foostruct struct {
  id int
  field1 int
  commonid int
}

type barstruct struct {
  id int
  field2 int
  commonid int
}

type fighterstruct struct {
  id int
  field3 int
  commonid int
}

1-它们都被保存在不同的切片中:sliceoffoostruct、sliceofbarstruct、sliceoffighterstruct

2-我正在迭代每个切片并将它们插入(未排序)到公共 var commonslice []interface{} 切片中

3-然后我需要按 commonid​​ 将它们排序到该切片中,这就是我陷入困境的地方。

我正在尝试做:

sort.Slice(commonSlice, func(i, j int) bool { return commonSlice[i].CommonID > commonSlice[j].CommonID })

但我收到错误

commonslice[i].commonid​​未定义(类型interface {}是没有方法的接口)

我也尝试过执行 commonslice[i].commonid​​.(int) 来转换类型,但它也不起作用。

也尝试过使用匿名结构和 commonid​​ 字段进行类似的操作,但没有成功。

我认为如果我直接转换正在比较的实际结构的类型,它就可以工作,但硬编码该类型将破坏“commonslice”的整个目的。

这是怎么做到的?我应该采取不同的做法吗?


解决方案


由于 commonslice 的元素类型是 interface{},因此您无法访问值的任何字段,因为它允许存储任何值,甚至不是结构体的值。

一种不推荐的方法是使用反射来访问 commonid​​ 字段,但这很丑陋且缓慢。作为参考,其外观如下:

all := []interface{}{
    foostruct{11, 22, 31},
    barstruct{21, 32, 33},
    fighterstruct{21, 32, 32},
}

sort.slice(all, func(i, j int) bool {
    commonid1 := reflect.valueof(all[i]).fieldbyname("commonid").int()
    commonid2 := reflect.valueof(all[j]).fieldbyname("commonid").int()
    return commonid1 > commonid2
})

fmt.println(all)

此输出(在 Go Playground 上尝试):

[{21 32 33} {21 32 32} {11 22 31}]

而是创建一个接口来描述访问 commonid​​ 字段的方法:

type hascommonid interface {
    getcommonid() int
}

并让你的struts实现这个接口:

func (f foostruct) getcommonid() int     { return f.commonid }
func (b barstruct) getcommonid() int     { return b.commonid }
func (f fighterstruct) getcommonid() int { return f.commonid }

并将您的值存储在此界面的一部分中:

all := []hascommonid{
    foostruct{11, 22, 31},
    barstruct{21, 32, 33},
    fighterstruct{21, 32, 32},
}

然后您可以使用 getcommonid​​() 方法在 less() 函数中访问它:

sort.slice(all, func(i, j int) bool {
    return all[i].getcommonid() > all[j].getcommonid()
})

这将输出相同的内容,请在 Go Playground 上尝试。

这更干净、更快、可扩展。

为了避免重复,您可以将公共字段和方法“外包”到一个结构体并将其嵌入到您的所有 strut 中:

type common struct {
    commonid int
}

func (c common) getcommonid() int { return c.commonid }

type foostruct struct {
    id     int
    field1 int
    common
}

type barstruct struct {
    id     int
    field2 int
    common
}

type fighterstruct struct {
    id     int
    field3 int
    common
}

注意:getcommonid​​()只定义一次,对于common,其他类型不需要添加。然后使用它:

all := []HasCommonID{
    FooStruct{11, 22, Common{31}},
    BarStruct{21, 32, Common{33}},
    FighterStruct{21, 32, Common{32}},
}

sort.Slice(all, func(i, j int) bool {
    return all[i].GetCommonID() > all[j].GetCommonID()
})

输出是一样的,在Go Playground上试试。

这还允许您在单个位置(common)扩展公共字段和方法列表,而无需进一步重复。

以上就是《空接口切片排序指南:如何对具有相同字段的空接口切片进行排序?》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>