golang返回多个值问题 [英] golang return multiple values issue
本文介绍了golang返回多个值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
func FindUserInfo(id string)(Info,bool){
it,present:= all [id]
return it,present
}
但这不是
func FindUserInfo(id string)(Info,bool){
return所有[id]
}
是否有避免临时变量的方法? p>
解决方案
您可以使用命名返回来保存几个按键:
func FindUserInfo(id string)(i Info,ok bool){
i,ok = all [id]
return
}
但除此之外,我不认为您想要的是什么。
I was wondering why this is valid go code:
func FindUserInfo(id string) (Info, bool) {
it, present := all[id]
return it, present
}
but this isn't
func FindUserInfo(id string) (Info, bool) {
return all[id]
}
is there a way to avoid the temporary variables?
解决方案
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.
这篇关于golang返回多个值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文