比较和排序在Go中

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

Comparison And Ordering in Go

问题

在Go语言中,有没有内部机制来实现相等性和排序?(这样我们就可以在类型上使用比较运算符 - ==,!=,<,>,<=,>=。)

注意:我看到一些类型有一个名为Less的方法,似乎用于排序。但我找不到关于它或相等性检查接口的文档(如果有的话)。

英文:

Is there any internal mechanism in Go for implementing equality and ordering? (So we can use comparison operators on the type - ==, !=, <, >, <=, >=.)

Note: I saw some types have a method named Less which seems to be used for ordering. But I can not find the documentation for that or for equality checking interface (if there is any).

答案1

得分: 7

Go不支持运算符重载,因此您无法使用您的类型覆盖这些运算符的行为。如果您需要在您的类型上使用这些操作,则将它们定义为方法。

您可能在某些类型上看到的Less方法可能是作为sort.Interface接口的一部分,或者可能是heap.Interface(它扩展了排序接口)。

英文:

Go does not support operator overloading, so you won't be able to override the behaviour of those operators with your type. If you need to use those operations on your type, then define them as methods.

The Less method you may have seen on some types is probably there as part of the sort.Interface interface or possibly heap.Interface (which extends the sort interface).

huangapple
  • 本文由 发表于 2013年3月20日 18:10:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/15520590.html
匿名

发表评论

匿名网友

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

确定