91 words, 1 min read

Initially, I was a bit sceptic when generics where introduced in Golang, but I'm slowly starting to love them.

Recently, I need to filter a slice and remove all duplicates. With generics, this is a breeze:

func UniqueT comparable []T {
inResult := make(map[T]bool)
var result []T
for _, str := range s {
if _, ok := inResult[str]; !ok {
inResult[str] = true
result = append(result, str)
}
}
return result
}

This is much easier than having to create the same function for each type you want to support.