拥有具有共同字段的结构...
type definition struct { id string ... } type requirement struct { id string ... } type campaign struct { id string ... }
...我有多个这样的函数:
func filldefinitionids(values *map[string]definition) { for key, value:=range *values { // repeated code value.id=key // repeated code (*values)[key]=value // repeated code } // repeated code } func fillrequirementids(values *map[string]requirement) { for key, value:=range *values { // repeated code value.id=key // repeated code (*values)[key]=value // repeated code } // repeated code } func fillcampaignids(values *map[string]campaign) { for key, value:=range *values { // repeated code value.id=key // repeated code (*values)[key]=value // repeated code } // repeated code }
我想要一个单一的函数,用泛型(或接口,等等)来概括访问,有点......
func fillIds[T Definition|Requirement|Campaign](values *map[string]T) { for key, value:=range *values { value.Id=key (*values)[key]=value } }
当然,这会导致 value.id 未定义(类型 t 没有字段或方法 id)
。我已经多次能够克服类似的问题,但这次我找不到解决方案。
如何将这组函数抽象为一个函数?
type definition struct { id string } type requirement struct { id string } type campaign struct { id string } func (v definition) withid(id string) definition { v.id = id; return v } func (v requirement) withid(id string) requirement { v.id = id; return v } func (v campaign) withid(id string) campaign { v.id = id; return v }
type withid[t any] interface { withid(id string) t } func fillids[t withid[t]](values map[string]t) { for key, value := range values { values[key] = value.withid(key) } }
func main() { m1 := map[string]definition{"foo": {}, "bar": {}} fillids(m1) fmt.println(m1) m2 := map[string]campaign{"abc": {}, "def": {}} fillids(m2) fmt.println(m2) }
https://go.dev/play/p/f3qk0gcykea
如果需要使用值映射,则可以替代@blackgreen的答案。
type common struct { id string } func (v *common) setid(id string) { v.id = id } type definition struct { common } type requirement struct { common } type campaign struct { common }
type idsetter[t any] interface { *t setid(id string) } func fillids[t any, u idsetter[t]](values map[string]t) { for key, value := range values { u(&value).setid(key) values[key] = value } }
func main() { m1 := map[string]definition{"foo": {}, "bar": {}} fillids(m1) fmt.println(m1) m2 := map[string]campaign{"abc": {}, "def": {}} fillids(m2) fmt.println(m2) }