...

/

Adding Type Parameters to Struct Types

Adding Type Parameters to Struct Types

Understand how we can utilize generics when dealing with type parameters in Go.

Earlier, we wrote a generic function for sorting slices called SortSlice(), but that has some limitations in that it can only handle slices that are based on types that meet the constraints in constraints.Ordered. Oftentimes, we might want to handle slices that might contain types based on struct, say, for example, this type:

Press + to interact
type Record struct {
First, Last string
}

Our SortSlice() function could not handle a []Record, so we need to do something different to handle these types of cases.

For this example, we want to use Go's built-in sort.Sort() function. This is a highly optimized sort that uses multiple sorting algorithms, depending on slice size.

To use it, we need a type that implements the sort.Interface type. That interface type is defined as follows:

Press + to interact
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}

Before Go generics, we would have needed to implement an adapter type to implement these for every type we wanted to sort. For example, here is an adapter to sort []int:

Press + to interact
type intAdapter struct {
sl []int
}
func (in intAdapter) Len() int {
return len(in.sl)
}
func (in intAdapter) Swap(i, j int) {
in.sl[i], in.sl[j] = in.sl[j], in.sl[i]
}
func (in intAdapter) Less(i, j int) bool {
return in.sl[i] < in.sl[j]
}

And we could use it like this:

Press + to interact
func main() {
ints := []int{5, 3, 7, 1}
sort.Sort(intAdapter{ints})
fmt.Println(ints)
}

We would then need to do this for every other signed type or other types we wanted to sort. Imagine doing this for all int8, int16, int32, and int64 signed integer types.

We would also need to do that for every other type we want to sort. So, what we want to do ...