好的,我有這個: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: "[email protected]", Password:"foo"} peopleById["2"] = Model{ID: 2, Handle: "jason",Firstname: "Jason", Lastname: "Statham", Email: "[email protected]", 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條經驗 獲得超13個贊
Go 中沒有結構可以幫助制作這個單行代碼。我認為,除了變量名中的下劃線,你現在擁有的是好的。
就個人而言,為了可讀性,我會添加更多行:
var?personInj?routes.PersonInjection personInj.PeopleById,?personInj.PeopleByHandle?=?models.PersonInit() handler.Mount(subRouter,?personInj)
- 1 回答
- 0 關注
- 157 瀏覽
添加回答
舉報
0/150
提交
取消