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

时间:2018-12-24 06:14:48

标签: go

好的,我有这个:

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 个答案:

答案 0 :(得分:4)

Go中没有任何构造可以帮助实现这种单线。我认为,除了underscores in the variable names以外,您现在所拥有的还可以。

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

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