在没有命名返回值的函数中使用裸返回的Go语言写法是什么?

huangapple go评论88阅读模式
英文:

golang naked return in function without named return

问题

我已经阅读了一些关于裸返回/具名返回的内容(这里)以及类似问题(这里),但对于裸返回仍然有一些疑问。以下是我正在使用的一个 REST API 书籍中的代码片段:

package main
import (
...
  "github.com/julienschmidt/httprouter"
)

func main() {
  router := httprouter.New()
  router.GET("/api/v1/go-version", showVersion)
  router.GET("/api/v1/show-file/:name", getFileContent)
  log.Fatal(http.ListenAndServe(":8000", router))
}
func getCommandOutput(command string, arguments ...string) string {
  out, _ := exec.Command(command, arguments...).Output()
  return string(out)
}

func showVersion(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  response := getCommandOutput("/usr/local/go/bin/go", "version")
  io.WriteString(w, response)
  return   /// 这个 "return" 是做什么用的?
}

func getFileContent(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  fmt.Fprintf(w, getCommandOutput("/bin/cat", params.ByName("name")))
}

为什么在 func showVersion 中使用了裸返回?

英文:

i have gone through some naked return/named returns(here) and similar question here but still have some doubts in my understanding on naked return. Below is code snippet from a rest api book im using

package main
import (
...
  "github.com/julienschmidt/httprouter"
)

func main() {
  router := httprouter.New()
  router.GET("/api/v1/go-version", showVersion)
  router.GET("/api/v1/show-file/:name", getFileContent)
  log.Fatal(http.ListenAndServe(":8000", router))
}
func getCommandOutput(command string, arguments ...string) string {
  out, _ := exec.Command(command, arguments...).Output()
  return string(out)
}

func showVersion(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  response := getCommandOutput("/usr/local/go/bin/go", "version")
  io.WriteString(w, response)
  return   /// what does this "return" give/do ?
}

func getFileContent(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  fmt.Fprintf(w, getCommandOutput("/bin/cat", params.ByName("name")))
}

why is the naked return used in func showVersion?

答案1

得分: 1

我不确定你选择的例子是否正确。showVersion中的return不是一个裸返回,因为showVersion没有返回任何内容。当函数签名中给出返回类型的标识符时,才会出现“裸返回”。返回值在函数声明时命名,并不需要在函数定义中声明。showVersion中的return不是裸返回,因为showVersion的声明没有命名结果。showVersion中的所有return语句只是告诉进程退出函数,甚至都不是必需的。

然而,关于命名结果和裸返回,它们主要是为了简洁和代码清晰性而存在(参见Effective Go)。你不必使用命名结果和裸返回。然而,文档建议使用它是“惯用法”,这是Go代码应该追求的。

然而,对于所有的建议,你需要决定它是否真正有效。命名结果是否真的使代码更可读?我认为是的,因为它更清楚地说明了函数的目的。我使用命名结果吗?也许不如我应该经常使用。

如果你想要一个明确的指南(超出Effective Go),可以查看Go库。它是否使用命名结果和裸返回?注意它们使用的频率和原因。当它们不使用时,试着弄清楚为什么不使用。你将获得最佳实践。你可能还想看一下流行的Go库(如gin)的存储库。由于社区的反馈,Go语言不断发展和改进。

英文:

I’m not sure you chose the correct example. The return in showVersionis not a naked reurn since showVersion doesn’t return anything. A “naked return” occurs when the return types are given identifiers in the function signature. The return values are named at function declaration and don’t have to be declared in the function definition. The return in showVersion is not naked as the declaration of showVersion does not have named results. All the return in showVersion does is tell the process to exit out of the function. It’s not even needed.

However, on the subject of named results and naked returns, they essentially exist for brevity and code clarity (see Effective Go). You don’t have to use named results and naked returns. However, the documentation suggests it is “idiomatic”, which is what go code should strive to be.

However, as with all pieces of advice you need to decide if it truly is effective coding. Do named results really make the code more readable? I think so, because it makes clearer what the purpose of the function is. Do I use named results? Perhaps, not as often as I should.

If you want a definitive guide (beyond Effective Go), take a look at the go library. Does it use named results and naked returns? Make a note of how often and why. When they don’t use them, try to figure out why not. You will glean best practices. You might also want to take a look at the repostories for popular go libraries like gin. The Go language is constantly evolving and improving thanks to community feedback.

huangapple
  • 本文由 发表于 2022年3月7日 12:22:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/71376387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定