【发布时间】:2016-04-22 09:58:59
【问题描述】:
我正在尝试提出一种非常紧凑的方法来处理可能在对 REST API 的 HTTP 请求中出现的问题。
我需要测试许多条件,以及针对任何这些条件失败的许多潜在错误响应。
我的处理流程类似于以下内容:
// Error is actually a method on some struct, so it's only relevant for demonstration purposes.
func Error(w http.ResponseWriter, status int, message string) {
// Lots of stuff omitted
w.WriteHeader(status)
w.WriteJson(r)
}
func HandleSomething(w http.ResponseWriter, r *http.Request) {
if someCondition != true {
Error(w, 500, "Some error occurred!")
return
}
if someOtherCondition != true {
Error(w, 500, "Some other error occurred!")
return
}
if yetAnotherCondition != true {
Error(w, 500, "Yet another error occurred!")
return
}
if andFinallyOneLastCondition != true {
Error(w, 500, "One final error occurred!")
return
}
// All good! Send back some real data.
w.WriteJson(someObject)
}
由于我经常需要测试 5-10 个条件,以及在其他操作期间可能出现的其他错误,因此能够将其压缩为以下内容会很不错:
func HandleSomething(w http.ResponseWriter, r *http.Request) {
if someCondition != true {
return Error(w, 500, "Some error occurred!")
}
if someOtherCondition != true {
return Error(w, 500, "Some other error occurred!")
}
if yetAnotherCondition != true {
return Error(w, 500, "Yet another error occurred!")
}
if andFinallyOneLastCondition != true {
return Error(w, 500, "One final error occurred!")
}
// All good! Send back some real data.
w.WriteJson(someObject)
}
但是,Go 编译器不喜欢这样。
它抱怨我试图将Error() 的结果用作值,并且我试图返回太多参数。确切的错误消息是:
foo.go:41: bar.Response.Error(400, "InvalidRequest", "Error decoding request") used as value
foo.go:41: too many arguments to return
但是Error() 和HandleSomething() 都具有相同的返回签名(例如,它们都没有返回任何内容),所以这不应该工作吗?
每个if 语句都包含一个return 很重要,因为该函数应该立即退出。如果Error() 可以以某种方式停止调用函数的执行,那也对我有用。有点像testing.FailNow(),但我相信这依赖于 Goroutines。
顺便说一句:我意识到这些并不是真正的“无效”函数,但想不出更合适的名称。 Go 中不返回任何内容的函数是否有合适的名称?
【问题讨论】:
-
go 中什么也不返回的函数就是这样。它没有名字,因为它本身并没有什么特别之处。