dongweiben5229
2018-12-24 06:14
浏览 18
已采纳

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

Ok so I have this:

handler.Mount(subRouter, routes.PersonInjection{PeopleById: models.PersonInit()})

PersonInit looks like:

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
}

the Map type is just Map[string]someStruct{}

and PersonInjection{} looks like:

type PersonInjection struct {
    PeopleById, PeopleByHandle person.Map
}

so I am looking to do something like:

handler.Mount(subRouter, routes.PersonInjection{PeopleById,PersonByHandle: models.PersonInit()...})

ummm anyone know how to do something like that?

for now I just have:

    by_id, by_handle := models.PersonInit()
    handler.Mount(subRouter, routes.PersonInjection{PeopleById: by_id, PeopleByHandle:by_handle})

图片转代码服务由CSDN问答提供 功能建议

好,所以我有这个:

  handler.Mount( 子路由器,路由。PersonInjection{PeopleById:models.PersonInit()})
   
 
 

PersonInit如下:

   func PersonInit()(地图,地图){
 
 peopleById [“ 1”] =模型{ID:1,句柄:“ alex”,名字:“ Alex”,姓氏:“ Chaz”,电子邮件:“ alex @  example.com“,密码:” foo“} 
 peopleById [” 2“] =型号{ID:2,句柄:” jason“,名字:” Jason“,姓氏:” Statham“,电子邮件:” jason @ example  .com“,密码:” foo“} 
 peopleByHandle [” alex“] = peopleById [” 1“] 
 peopleByHandle [” jason“] = peopleById [” 2“] 
 
返回peopleById,peopleByHandle 
  } 
   
 
 

地图类型仅为 Map [string] someStruct {}

和< code> PersonInjection {} 看起来像:

  type PersonInjection结构{
 PeopleById,PeopleByHandle person.Map 
} 
   
 
 

,所以我想做类似的事情:

  handler.Mount(subRouter,  route.PersonInjection {PeopleById,PersonByHandle:models.PersonInit()...})
   
 
 

嗯,有人知道该怎么做吗? \ n

现在我只有:

  by_id,by_handle:= models.PersonInit()
 handler.Mount(subRouter,routes.PersonInjection {PeopleById:  by_id,PeopleByHandle:by_handle})
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsfdsf48652 2018-12-24 07:03
    已采纳

    There is no construction in Go which would help to make this one-liner. I think, what you have now is OK, except the underscores in the variable names.

    Personally, I would add even more lines for the sake of readability:

    var personInj routes.PersonInjection
    personInj.PeopleById, personInj.PeopleByHandle = models.PersonInit()
    handler.Mount(subRouter, personInj)
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题