While VonC's answer is probably closest to what you really want, the only real way to do it in native Go without gen is to define an interface
type IDList interface { // Returns the id of the element at i ID(i int) int // Returns the element // with the given id GetByID(id int) interface{} Len() int // Adds the element to the list Insert(interface{}) } // Puts the deduplicated list in dst func Deduplicate(dst, list IDList) { intList := make([]int, list.Len()) for i := range intList { intList[i] = list.ID(i) } uniques := uniq(intList) for _,el := range uniques { dst.Insert(list.GetByID(el)) } }
Where uniq is a function of your OP.
This is just one possible example, and probably much better, but in the general case, matching each item with a unique identifier "== able" and either creating a new list or filtering based on identifier deduplication is probably the most intuitive way.
An alternative solution is to take []IDer , where the IDer interface is equal to ID() int . However, this means that custom code must create a list of [] IDer and copy all the elements into this list, which is a bit ugly. It is cleaner for the user to wrap the list as a list of identifiers rather than copy, but this is a similar amount of work anyway.
LinearZoetrope
source share