type S struct {
ID int64
Name string
}
func (s S) PrimaryKey() int64 {
return
s.ID}
type PrimaryKey interface {
PrimaryKey() int64
}
func TestStructSliceToMap(t *testing.T) {
list := []PrimaryKey{S{ID: 1, Name: "string1"}, S{ID: 2, Name: "string2"}}
fmt.Println(StructSliceToMap(list))
}
func StructSliceToMap(list []PrimaryKey) (smap map[int64]PrimaryKey) {
smap = make(map[int64]PrimaryKey)
if len(list) == 0 {
return
}
for i := range list {
smap[list[i].PrimaryKey()] = list[i]
}
return
}