Structs are already comparable, but all fields must be the same:
https://go.dev/play/p/XwhSz4DEDwL

I think your solution with function 'eq' is fine.  You can see the same 
thing in the standard library in slices.CompactFunc and slices.EqualFunc
https://pkg.go.dev/slices#CompactFunc
https://pkg.go.dev/slices#EqualFunc

For the case of "ordered" rather than "comparable", have a look at 
slices.BinarySearchFunc and related functions.

On Thursday, 13 July 2023 at 09:29:38 UTC+1 Mark wrote:

> I have a package which has a function `Do[T comparable](a, b []T) Result`.
> I have a struct:
> ```go
> type N struct {
>   x int
>   y int
>   t string
> }
> ```
> Is it possible to make `N` comparable; in particular by a field of my 
> choice, e.g., `t`?
>
> Or will I have to make, say, `DoFunc(a, b []N, eq func(i, j N) bool) 
> Result` with, say,
> `func eq(i, j N) { return i.t == j.t }`?
>

-- 
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 on the web visit 
https://groups.google.com/d/msgid/golang-nuts/a697e28e-4d38-4c1a-b0b7-dccecb1c4309n%40googlegroups.com.

Reply via email to