为了账号安全,请及时绑定邮箱和手机立即绑定

从返回元组的 func 初始化结构

从返回元组的 func 初始化结构

Go
繁花如伊 2023-05-22 16:07:20
好的,我有这个:handler.Mount(subRouter, routes.PersonInjection{PeopleById: models.PersonInit()})PersonInit 看起来像:func PersonInit() (Map,Map) {    peopleById["1"] = Model{ID: 1, Handle: "alex", Firstname: "Alex", Lastname: "Chaz", Email: "alex@example.com", Password:"foo"}    peopleById["2"] = Model{ID: 2, Handle: "jason",Firstname: "Jason", Lastname: "Statham", Email: "jason@example.com", Password:"foo"}    peopleByHandle["alex"] = peopleById["1"]    peopleByHandle["jason"] = peopleById["2"]    return peopleById, peopleByHandle}地图类型只是Map[string]someStruct{}看起来PersonInjection{}像:type PersonInjection struct {    PeopleById, PeopleByHandle person.Map}所以我想做类似的事情:handler.Mount(subRouter, routes.PersonInjection{PeopleById,PersonByHandle: models.PersonInit()...})嗯,有人知道怎么做吗?现在我只有:    by_id, by_handle := models.PersonInit()    handler.Mount(subRouter, routes.PersonInjection{PeopleById: by_id, PeopleByHandle:by_handle})
查看完整描述

1 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

Go 中没有结构可以帮助制作这个单行代码。我认为,除了变量名中的下划线,你现在拥有的是好的。

就个人而言,为了可读性,我会添加更多行:

var personInj routes.PersonInjection
personInj.PeopleById, personInj.PeopleByHandle = models.PersonInit()
handler.Mount(subRouter, personInj)


查看完整回答
反对 回复 2023-05-22
  • 1 回答
  • 0 关注
  • 116 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信