K
K
KirSupi2022-04-17 15:15:59
go
KirSupi, 2022-04-17 15:15:59

How to implement custom type comparison in Go?

For example, in Python, to compare two class objects through == or >, it is enough for this class to set the methods __eq__(self, other) (equals), __gt__(self, other) (greater)
Is there something similar in Go, or do you have to do a.Greater(b) ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2022-04-17
@vabka

There is no way in Go to override standard operators or define them for your own types.
So yeah

have to do a.Greater(b)

U
uvelichitel, 2022-04-18
@uvelichitel

There is also reflect. deepequal to not implement the interface yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question