With a helper function, you can.
Create a helper function which returns the second bool
return value, e.g.:
func check(dummy interface{}, ok bool) bool {
return ok
}
And using it:
if check(f()) && check(g()) && h() && check(i()) {
doStuff()
}
Note that this is equivalent to the original code because the &&
operator is evaluated from left to right and it is using short-circuit evaluation: if any of the operands evaluate to false
, further operands (functions) will not be called.
This check()
function works for all functions that return 2 values and the 2nd is of type bool
(because any value can be assigned to a variable of type interface{}
).
This is covered in the Spec: Calls:
As a special case, if the return values of a function or method g
are equal in number and individually assignable to the parameters of another function or method f
, then the call f(g(parameters_of_g))
will invoke f
after binding the return values of g
to the parameters of f
in order. The call of f
must contain no parameters other than the call of g
, and g
must have at least one return value. If f
has a final ...
parameter, it is assigned the return values of g
that remain after assignment of regular parameters.
Note: since the first parameter in check()
is not used, we can even use the blank identifier when naming it which will make it obvious that we don't use that parameter:
func check(_ interface{}, ok bool) bool {
return ok
}