英文:
What the different between those two Golang function return
问题
这两个 Golang 函数有什么不同,它们彼此相同吗?
func foo1() (ret string) {
ret = "hi there"
return
}
func foo2() string {
ret := "hi there"
return ret
}
哪个更好?
英文:
What the different between those two Golang functions, are they the same with each other?
func foo1() (ret string) {
ret = "hi there"
return
}
func foo2() string {
ret := "hi there"
return ret
}
which is better?
答案1
得分: 3
这两个函数是相同的:https://go.dev/play/p/_6KT5thL2Sj
-
foo2
使用隐式返回。 -
foo1
使用命名返回值和naked/bare
返回。有些人认为这是一种代码异味:提案:Go 2:移除裸返回
英文:
These two functions are identical: https://go.dev/play/p/_6KT5thL2Sj
-
foo2
uses implicit return. -
foo1
uses named return values andnaked/bare
return. Some consider this a code smell: proposal: Go 2: remove bare return
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论