You seem to be saying "if the S field is different then I want to consider 
these two structs different, and get pointers to the two structs. If the S 
field is the same then I want to skip the pair entirely". Is that right?

The required semantics are not entirely clear, but it sounds like a handful 
of lines of code to implement - there's no point importing and learning a 
third party library.

On the assumption that all the elements to be compared are in corresponding 
positions in a and b:
https://go.dev/play/p/Y71sLUpftzR

On Friday, 14 July 2023 at 09:11:35 UTC+1 Mark wrote:

> In fact the diff pkg mentioned above does work but is of no use to me 
> since for each change it gives back only the field(s) used, not the 
> original structs (or pointers to them), so I can't see any way back to the 
> original structs (or their slice indexes).
>
> On Friday, July 14, 2023 at 8:58:41 AM UTC+1 Mark wrote:
>
>> What I really want to do is to be able to diff slices of structs on the 
>> basis of one single field.
>> For example, given:
>> ```
>> type Item struct {
>>   I int
>>   S string
>> }
>> ```
>> and given `a` and `b` are both of type`[]Item`, I want to diff these 
>> slices based purely on the `S` field, ignoring the `I` field.
>>
>> This diff pkg <https://pkg.go.dev/github.com/r3labs/diff/v3> claims to 
>> be able to do this (something I'm testing, so I don't know either way yet), 
>> but in any case, it is incredibly slow.
>>
>> On Friday, July 14, 2023 at 8:31:39 AM UTC+1 Peter Galbavy wrote:
>>
>>> As a slight digression - I thought I was going mad, but 'slices' and 
>>> 'maps' are new :-) Only in 1.21 though...
>>>
>>> Well, there is a lot of boiler plate that maps.Keys() will get rid of.
>>>
>>> On Thursday, 13 July 2023 at 10:06:01 UTC+1 Brian Candler wrote:
>>>
>>>> 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/e3bae2b0-6f0b-4302-b7e3-04103954d361n%40googlegroups.com.

Reply via email to