V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  flywheel  ›  全部回复第 1 页 / 共 1 页
回复总数  2
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
}
2020-09-18 11:06:35 +08:00
回复了 dafsic 创建的主题 Go 编程语言 看到好多人吐槽 golang 的错误处理,但我用的很爽啊
不做特殊处理,方便理解及代码阅读
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5430 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 03:48 · PVG 11:48 · LAX 20:48 · JFK 23:48
Developed with CodeLauncher
♥ Do have faith in what you're doing.