Named return values----Go指名道姓的函数返回值

原文

Go's return values may be named. If so, they are treated as variables defined at the top of the function. These names should be used to document the meaning of the return values. A return statement without arguments returns the named return values. This is known as a "naked" return. Naked return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions. 复制代码

代码

package main
import "fmt"

func split(sum int) (x, y int) {
	x = sum * 4 / 9
	y = sum - x
	return
}

func main() {
	fmt.Println(split(17))
}
复制代码

翻译

Go函数的返回值能够指名道姓。这些被指名道姓的返回值会当作变量声明在函数的顶部
这些名称应用于记录返回值的含义
一个return语句后面啥都不写,就像一个裸体return同样

注意:裸return语句应该仅仅用在很短的函数内,就像这个例子同样,在长函数当中,具备必定的损害可读性
复制代码

总结

整体来讲,这个不建议使用,仍是别指名道姓了,读取来挺费劲的。
复制代码
相关文章
相关标签/搜索