golang return multiple values issue

后端 未结 5 1042
陌清茗
陌清茗 2021-02-05 08:47

I was wondering why this is valid go code:

func FindUserInfo(id string) (Info, bool) {
    it, present := all[id]
    return it, present
}

but

5条回答
  •  心在旅途
    2021-02-05 09:47

    You may save a couple of key strokes by using named returns:

    func FindUserInfo(id string) (i Info, ok bool) {
        i, ok = all[id]
        return
    }
    

    But apart from that, I don't think what you want is possible.

提交回复
热议问题