我可以在 Go/Golang if 块的开头初始化多个变量,使用具有多个 return 值的多个函数吗?

Can I initialise multiple variables at the beginning of a Go/Golang if block using multiple functions with multiple return values?

我正在尝试使用两个函数来初始化 if 块内的变量。 两个函数 return 一个 bool 值和一个错误。 我尝试了以下,但没有成功。有没有办法做我想做的事?

func returnAValueAndError() (bool, error) {
    return true, nil
}

func alsoReturnAValueAndError() (bool, error) {
    return false, nil
}

if a, _ := returnAValueAndError(), b, _ := alsoReturnAValueAndError(); a || b {
//    this gives me
//    ./prog.go:16:42: syntax error: unexpected :=, expecting {
}

if a, _ := returnAValueAndError(); b, _ := alsoReturnAValueAndError(); a || b {
//    this gives me
//    ./prog.go:16:42: syntax error: cannot use b, _ := alsoReturnAValueAndError() as value
//    ./prog.go:17:38: syntax error: unexpected newline, expecting comma or }
}

if a, _, b, _ := returnAValueAndError(), alsoReturnAValueAndError(); a || b {
//    this gives me
//    ./prog.go:16:16: assignment mismatch: 4 variables but returnAValueAndError returns 2 values
//    ./prog.go:16:39: multiple-value returnAValueAndError() in single-value context
//    ./prog.go:16:67: multiple-value alsoReturnAValueAndError() in single-value context
}

你不能那样做。条件前面可以有一个简单的语句(不是多个语句):

https://golang.org/ref/spec#If_statements