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})
  • 写回答

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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题