Go 返回带有另一个空函数结果的空函数?
我试图提出一种非常简洁的方法来处理RESTAPI HTTP请求中可能出现的问题 我需要测试许多条件,以及任何这些条件失败时的许多潜在错误响应 我的处理流程如下所示:Go 返回带有另一个空函数结果的空函数?,go,idioms,Go,Idioms,我试图提出一种非常简洁的方法来处理RESTAPI 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) { // Lo
// 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)
}
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)
}
由于我通常需要测试5-10个条件,以及在其他操作期间可能出现的其他错误,因此最好将其压缩为以下内容:
// 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)
}
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
顺便说一句:我意识到这些函数并不是真正的“void”函数,但想不出更合适的名称。对于在Go中不返回任何内容的函数,是否有一个合适的名称?C++允许这样做。它对于模板非常方便 但正如你所看到的,Go并没有这样做。我不知道具体的步骤,但这似乎是你可以要求添加到去的东西,也许它可以在1.7
至于现在的解决方案,也许您可以返回一个未使用的
错误
。只需从Error
函数中将其返回为nil
。C++允许这样做。它对于模板非常方便
但正如你所看到的,Go并没有这样做。我不知道具体的步骤,但这似乎是你可以要求添加到去的东西,也许它可以在1.7
至于现在的解决方案,也许您可以返回一个未使用的
错误
。只需从Error
函数返回nil
。我不确定,但我会使用if-else方式
func HandleSomething(w http.ResponseWriter, r *http.Request) {
if someCondition != true {
Error(w, 500, "Some error occurred!")
} else if someOtherCondition != true {
Error(w, 500, "Some other error occurred!")
} else if yetAnotherCondition != true {
Error(w, 500, "Yet another error occurred!")
} else if andFinallyOneLastCondition != true {
Error(w, 500, "One final error occurred!")
} else {
// All good! Send back some real data.
w.WriteJson(someObject)
}
}
我不确定,但我会用if else的方式来做
func HandleSomething(w http.ResponseWriter, r *http.Request) {
if someCondition != true {
Error(w, 500, "Some error occurred!")
} else if someOtherCondition != true {
Error(w, 500, "Some other error occurred!")
} else if yetAnotherCondition != true {
Error(w, 500, "Yet another error occurred!")
} else if andFinallyOneLastCondition != true {
Error(w, 500, "One final error occurred!")
} else {
// All good! Send back some real data.
w.WriteJson(someObject)
}
}
go语言基本上是按照您所看到的那样完成的,并且更改有一个非常高的接受标准。不太可能接受对语言规范进行例外以基本上保存新行字符。@JimB:Go允许空返回。它只是不允许从另一个函数返回空返回。这对我来说似乎不是什么大变化。至少可以尝试看看是否可以进行更改。@DC\uA:可能包含0或更多。表达式根据定义生成值。同样在规范中:“在没有结果类型的函数中,“return”语句不能指定任何结果值”。即使是很小的语言变化也可能会产生意想不到的后果,需要在语言的生命周期中保持下去,而且将两行压缩成一行可能要付出很大的代价(注意,像合并for+select以保存一行这样的提议也被拒绝了)。正如您所看到的,围棋语言已经基本完成了,而变革有着极高的被接受标准。不太可能接受对语言规范进行例外以基本上保存新行字符。@JimB:Go允许空返回。它只是不允许从另一个函数返回空返回。这对我来说似乎不是什么大变化。至少可以尝试看看是否可以进行更改。@DC\uA:可能包含0或更多。表达式根据定义生成值。同样在规范中:“在没有结果类型的函数中,“return”语句不能指定任何结果值”。即使是很小的语言更改也可能会产生意想不到的后果,需要在语言的整个生命周期中加以维护,而将两行压缩为一行可能需要付出巨大的代价(请注意,像组合for+select以保存一行这样的建议也被拒绝)在go中不返回任何内容的函数就是这样。它没有名字,因为它本身并没有什么特别之处。一个在go中不返回任何内容的函数就是这样。它没有名字,因为它本身没有什么特别之处。