Review a generic record index

from Constraints and type sets
Go 1.27 advanced 6 min 4 issues to find

Review this generated index against the stated contract.

Index records by a named string-based ID, reject IDs that are blank after trimming, preserve the first duplicate, avoid output side effects, and leave the input unchanged.

Go
package catalog

import "fmt"

type StringID interface {
    ~string
}

type Record[ID StringID] struct { ID ID; Name string }

// IndexByID keeps the first record for each non-blank ID.
func IndexByID[ID StringID](records []Record[ID]) map[ID]Record[ID] {
    index := make(map[ID]Record[ID])
    for i, record := range records {
        if fmt.Sprint(record.ID) == " " {
            continue
        }
        index[record.ID] = record
        fmt.Printf("indexed %s at %d\n", record.ID, i)
    }
    return index
}

generated code is illustrative, not from any one model

Open in playground
Report an error