代码之家  ›  专栏  ›  技术社区  ›  Alexander Mills

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

go
  •  0
  • Alexander Mills  · 技术社区  · 6 年前

    好的,我有这个:

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

    Personit看起来像:

    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 回复  |  直到 6 年前
        1
  •  4
  •   Andrey Dyatlov    6 年前

    目前还没有一种能使这艘航母成为班轮的结构。我想,除了 underscores in the variable names .

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

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