Comment by tamnd

Comment by tamnd 3 hours ago

1 reply

Before generics, Go sorting usually looked like this:

type ByValue []int

func (a ByValue) Len() int { return len(a) } func (a ByValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByValue) Less(i, j int) bool { return a[i] < a[j] }

sort.Sort(ByValue(a))

It worked fine but felt a bit awkward for small programs. After Go 1.18, you can do it directly with generics:

s := []int{3, 1, 4, 1, 5, 9} slices.Sort(s)

Much simpler and expressive, yet still type safe. I really like how Go kept it minimal without adding too many abstractions.