Review generated stable deduplication

from Generics
Go 1.27 advanced 6 min 4 issues to find

Review this generated stable deduplication helper against the stated task.

Return the first occurrence of each value in input order, preserve nil input as nil, and do not mutate the input.

Go
package cache

import (
    "fmt"
    "sort"
)

// Unique makes output deterministic.
func Unique[T comparable](values []T) []T {
    if values == nil {
        return []T{}
    }
    seen := map[T]bool{}
    result := []T{}
    for _, value := range values {
        if !seen[value] {
            seen[value] = true
            result = append(result, value)
        }
    }
    sort.Slice(result, func(i, j int) bool {
        return fmt.Sprint(result[i]) < fmt.Sprint(result[j])
    })
    return result
}

generated code is illustrative, not from any one model

Open in playground
Report an error