Kaveh Shahbazian
Kaveh Shahbazian

Reputation: 13513

Comparison And Ordering in Go

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).

Upvotes: 3

Views: 1265

Answers (1)

James Henstridge
James Henstridge

Reputation: 43899

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).

Upvotes: 7

Related Questions