英文:
How do you append function result, while overwriting error?
问题
通常使用result, err := func()
。
当其中一个变量已经初始化时:
_, err := func()
var result string
result, err = func()
执行:
result, err = func()
all_results += result // 看起来多余和不必要
如何将结果附加到其中一个变量(result),并重置另一个变量?
// 类似于这样:
var result slice
// for 循环 {
result, _ += func() // 结合这一行
_, err = func() // 与这一行
你可以这样做吗:
result +=, err = func()
// 或者
result, err +=, = func()
// 或者
result, err += = func()
// 或者
result, err (+=, =) func() // ?
英文:
Usually, result, err := func()
is used.
When one of the variables is already initialized:
_, err := func()
var result string
result, err = func()
Doing:
result, err = func()
all_results += result // seems redundant and unneeded
How do you append results to one of them (result), and reset the other one?
// along the lines of this:
var result slice
// for loop {
result, _ += func() // combine this line
_, err = func() // with this line
Can you do:
result +=, err = func()
// or
result, err +=, = func()
// or
result, err += = func()
// or
result, err (+=, =) func() // ?
答案1
得分: 2
语言规范不支持对多个返回值进行不同处理。
然而,可以通过一个辅助函数很容易实现:
func foo() (int, error) {
return 1, nil
}
func main() {
var all int
add := func(result int, err error) error {
all += result
return err
}
if err := add(foo()); err != nil {
panic(err)
}
if err := add(foo()); err != nil {
panic(err)
}
if err := add(foo()); err != nil {
panic(err)
}
fmt.Println(all)
}
这将输出 3
(在 Go Playground 上尝试)。
如果你可以将错误处理移到辅助函数中,代码也可以这样写:
var all int
check := func(result int, err error) int {
if err != nil {
panic(err)
}
return result
}
all += check(foo())
all += check(foo())
all += check(foo())
fmt.Println(all)
这个输出结果相同,可以在 Go Playground 上尝试。
另一种变体是在辅助函数中完成所有操作:
var all int
handle := func(result int, err error) {
if err != nil {
panic(err)
}
all += result
}
handle(foo())
handle(foo())
handle(foo())
fmt.Println(all)
可以在 Go Playground 上尝试这个版本。
参考链接:https://stackoverflow.com/questions/28227095/multiple-values-in-single-value-context/28233172#28233172
英文:
The language spec does not support different treatment for multiple return values.
However, it's very easy to do it with a helper function:
func foo() (int, error) {
return 1, nil
}
func main() {
var all int
add := func(result int, err error) error {
all += result
return err
}
if err := add(foo()); err != nil {
panic(err)
}
if err := add(foo()); err != nil {
panic(err)
}
if err := add(foo()); err != nil {
panic(err)
}
fmt.Println(all)
}
This will output 3
(try it on the Go Playground).
If you can move the error handling into the helper function, it can also look like this:
var all int
check := func(result int, err error) int {
if err != nil {
panic(err)
}
return result
}
all += check(foo())
all += check(foo())
all += check(foo())
fmt.Println(all)
This outputs the same, try this one on the Go Playground.
Another variant can be to do everything in the helper function:
var all int
handle := func(result int, err error) {
if err != nil {
panic(err)
}
all += result
}
handle(foo())
handle(foo())
handle(foo())
fmt.Println(all)
Try this one on the Go Playground.
See related: https://stackoverflow.com/questions/28227095/multiple-values-in-single-value-context/28233172#28233172
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论