This is basically the generics question: why can't you write a generic
function that operates on slices independently of the type of the
slice element? In the absence of generics, the function has to be
written using an empty interface, which provides no compile-time type
checking. So people just write the loop.
Using the design draft at
https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
this would be written as
func Contains(type T comparable)(s []T, x T) bool {
for _, v := range s {
if v == x {
return true
}
}
return false
}
Ian