如何在Golang中将函数扩展到导入的类型上

huangapple go评论83阅读模式
英文:

How to extend functions onto imported types in Golang

问题

我有一个导入的类型

type ExternalType struct {
   quantity int
}


type ExternalArray []*ExternalType

我想要能够为 ExternalArray 实现排序接口,以便按照 quantity 进行排序。

然而,我不确定该如何做到这一点?

一个具体的示例是这样的:

https://play.golang.org/p/bEPtJ8NHQK

英文:

I have an imported type

type ExternalType struct {
   quantity int
}


type ExternalArray []*ExternalType

I want to be able to implement the sort interface for ExternalArray for so that I sort it by quantity.

However, I am not sure how I could do that?

A concrete example is this:

https://play.golang.org/p/bEPtJ8NHQK

答案1

得分: 7

sort.Interface 定义了必须实现的三个方法:

// Len 返回集合中的元素数量。
Len() int

// Less 报告索引为 i 的元素是否应该在索引为 j 的元素之前排序。
Less(i, j int) bool

// Swap 交换索引为 i 和 j 的元素。
Swap(i, j int)

在这个上下文中,可以这样实现:

type ExternalType struct {
   quantity int
}

type ExternalArray []*ExternalType

func (ea ExternalArray) Len() int {
    return len(ea)
}

func (ea ExternalArray) Less(i, j int) bool {
	return ea[i].quantity < ea[j].quantity
}

func (ea ExternalArray) Swap(i, j int) {
    ea[i], ea[j] = ea[j], ea[i]
}

为了进行排序,你可以使用 sort.Sort,例如:

arr := ExternalArray{
	&ExternalType{quantity: 33},
	&ExternalType{quantity: 44},
	&ExternalType{quantity: 22},
	&ExternalType{quantity: 11},
}

sort.Sort(arr)
// `arr` 现在已经排序好了 :-)

这里 是在 playground 中的一个可运行示例。

英文:

The sort.Interface defines three methods which must be implemented:

// Len is the number of elements in the collection.
Len() int

// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool

// Swap swaps the elements with indexes i and j.
Swap(i, j int)

In this context this would look something like:

type ExternalType struct {
   quantity int
}

type ExternalArray []*ExternalType

func (ea ExternalArray) Len() int {
    return len(ea)
}

func (ea ExternalArray) Less(i, j int) bool {
	return ea[i].quantity &lt; ea[j].quantity
}

func (ea ExternalArray) Swap(i, j int) {
    ea[i], ea[j] = ea[j], ea[i]
}

In order to do the sort you can then use sort.Sort, for example:

arr := ExternalArray{
	&amp;ExternalType{quantity: 33},
	&amp;ExternalType{quantity: 44},
	&amp;ExternalType{quantity: 22},
	&amp;ExternalType{quantity: 11},
}

sort.Sort(arr)
// `arr` is now sorted :-)

Here is a working example in the playground.

答案2

得分: 5

在当前包中定义一个类型,该类型可以对与导入类型具有相同元素类型的切片进行排序:

type byQuantity []*pkg.ExternalType

func (a byQuantity) Len() int           { return len(a) }
func (a byQuantity) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a byQuantity) Less(i, j int) bool { return a[i].Quantity < a[j].Quantity }

将导入的切片类型值转换为上述定义的类型,并进行排序:

a := pkg.ExternalArray{{1}, {3}, {2}}
sort.Sort(byQuantity(a))
// a现在按数量排序

由于原始切片和转换后的切片共享相同的底层数组对转换后的切片进行排序也会对原始切片进行排序

playground示例[链接](http://play.golang.org/p/34Le_ERxdR)
英文:

Define a type in the current package that sorts a slice with the same element type as the imported type:

type byQuantity []*pkg.ExternalType

func (a byQuantity) Len() int           { return len(a) }
func (a byQuantity) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a byQuantity) Less(i, j int) bool { return a[i].Quantity &lt; a[j].Quantity }

Convert the imported slice type value to the type defined above and sort:

a := pkg.ExternalArray{{1}, {3}, {2}}
sort.Sort(byQuantity(a))
// a is now sorted by quantity

Because the original slice and the converted slice share the same backing array, sort on the converted slice also sorts the original slice.

playground example

huangapple
  • 本文由 发表于 2016年2月17日 05:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/35442911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定