英文:
In Golang, can I assign slice values as return values?
问题
我正在尝试为错误检查开发一个透传函数,其中某些参数会被评估,其余参数会被返回。但我希望这些参数能够作为多个返回值而不是一个切片返回。在Go语言中有没有办法实现这个功能呢?以下是一个示例:
func Check(args ...interface{}) ...interface{} {
last := len(args) - 1
err := args[last]
// 检查最后一个参数是否为错误
if err != nil {
panic(err)
}
// 返回被检查函数返回的任何参数
return ...args[:last]
}
我知道这个函数声明的格式不太正确。这只是为了举例说明。理想情况下,我希望能够返回可变数量的值,然后通过赋值在另一端接收这些值。这将允许在我想要使用标准的错误/恐慌习语时进行简单的内联错误检查。
我知道我可以返回切片,然后将其部分分配给单独的变量,或者我可以创建多个这样的函数(例如Check0、Check1、Check2等),每个函数具有不同数量的返回值,但这些解决方案都不太优雅。有没有关于如何优雅地实现这样的功能的想法?或者在Go的当前阶段是否不可能实现?
在相关的问题上,有没有人知道是否有计划使切片能够解包到变量中,类似于以下的写法?
one, two, three := []string{"one", "two", "three"}
英文:
I am attempting to develop a passthrough function for error checking where certain arguments are evaluated, and the rest are returned. But I would like these to be returned as multiple return values rather than a slice. Is there any way to do this in Go? Here's an example:
func Check(args ...interface{}) ...interface{} {
last := len(args) - 1
err := args[last]
// Check for an error in the last argument
if err != nil {
panic(err)
}
// Return any args returned by the function we're checking
return ...args[:last]
}
I know this isn't quite formated right in the function declaration. This is just for the sake of argument. I would ideally like to be able to return a variable number of values, which could then be received on the other side via assignment. This would allow for simple inline error checking when I want to use the standard err/panic idiom.
I know that I could return the slice instead and then assign it's parts to individual variables, or I could create multiple such functions (e.g. Check0, Check1, Check2, etc.), each having a distinct number or return values, but neither of these solutions is very elegant. Any ideas on how to make something like this work gracefully? Or is it just not possible at this stage of Go?
On a related note, does anyone know if there are any plans to make slices unpackable into variables, something like the following?
one, two, three := []string{"one", "two", "three"}
答案1
得分: 1
你不能这样做,我认为这甚至没有计划,这是一件好事。你可以尝试像这样做(这很丑陋,不应该使用):
func Check(args ...interface{}) []interface{} {
if err := args[len(args)-1]; err != nil {
// 处理错误
}
args = args[:len(args)-1]
return args
}
func Check2i(args ...interface{}) (int, int) {
return args[0].(int), args[1].(int)
}
func main() {
fmt.Println(Check(10, 20, 30, nil)...)
a, b := Check2i(Check(10, 20, nil)...)
_, _ = a, b
}
英文:
You can't do that, I don't think that's even planned, which is a good thing IMO.
Your option is doing something like this (this is ugly, and shouldn't be used):
func Check(args ...interface{}) []interface{} {
if err := args[len(args)-1]; err != nil {
//do suff with err
}
args = args[:len(args)-1]
return args
}
func Check2i(args ...interface{}) (int, int) {
return args[0].(int), args[1].(int)
}
func main() {
fmt.Println(Check(10, 20, 30, nil)...)
a, b := Check2i(Check(10, 20, nil)...)
_, _ = a, b
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论