You have to remember that methods are just regular functions with the 
receiver as the first parameter. From the spec 
(https://go.dev/ref/spec#Method_declarations):

> A method is a function <https://go.dev/ref/spec#Function_declarations> with 
a receiver.

This mean that if you detach the method from the receiver, which 
time.Time.Compare does, then it requires two arguments instead of one.

a := time.Now()
b := a.Add(1 * time.Hour)
cmp := time.Time.Compare
cmp(a) // error: not enough arguments in call to cmp
cmp(a, b) // ok

https://go.dev/play/p/x3ZGIhQ2TNE


Diogo Lisboa

On Thursday, February 20, 2025 at 2:57:35 PM UTC-3 cpu...@gmail.com wrote:

> Sorry for not finding a better than this click bait subject. 
>
> In https://github.com/golang/go/issues/62642 this suggestion was made:
>
> slices.SortFunc(times, time.Time.Compare)
>
> It's totally unclear to me how Time.Compare matches the compare func(a,b 
> T) int signature? I assume it's something from the golang spec, but which 
> part?
>
> Are there other typical uses of this capability that are common?
>
> Cheers,
> Andi
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/4fbd818f-b324-4b78-8d95-667529d36c8dn%40googlegroups.com.

Reply via email to