专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

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语句应该仅仅用在很短的函数内,就像这个例子一样,在长函数当中,具有一定的损害可读性

总结

总体来说,这个不建议使用,还是别指名道姓了,读取来挺费劲的。

文章永久链接:https://tech.souyunku.com/44581

未经允许不得转载:搜云库技术团队 » Named return values----Go指名道姓的函数返回值

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们