Return Unique items in a Go slice I'm trying to return unique items in a Go slice. I've landed on the following, but it seems rather sub-optimal. I'm new to golang (first project in it) and wondering if there's a better way to approach this?

// 
// Returns unique items in a slice
//
func Unique(strSlice []string) []string {
    keys := make(map[string]bool)
    list := []string{} 
    for _, entry := range strSlice {
        if _, value := keys[entry]; !value {
            keys[entry] = true
            list = append(list, entry)
        }
    }    
    return list
}
This is not idiomatic writing style in Go, because the value variable is misleading:


if _, value := keys[entry]; !value {



The first returned value is the value in the map, the second value indicates success or failure of the lookup.
So rename it to ok or found.

If the slice is very large,
then list = append(list, entry) may lead to repeated allocations.
In that case, you can optimize by preallocating list to the maximum possible capacity (len(strSlice)), and assign elements by index rather than using append.
But if you have no reason for this optimization then don't do it,
that would be premature optimization,
the current solution is fine.