Its a compiler bug, make the function look like:
func somefunc(saddr string) (FinalResultType, os.Error) {
if result, err := doSomeCalculation(); e != nil {
return nil, e
}
return wrapIt(result), nil
}
grtz,
--
Miek
The compilers require either a return or a panic to be lexically last in a function with a result. This rule is easier than requiring full flow control analysis to determine whether a function reaches the end without returning (which is very hard in general), and simpler than rules to enumerate easy cases such as this one. Also, being purely lexical, the error cannot arise spontaneously due to changes in values such as constants used in control structures inside the function.
-rob
> [ Quoting <jos...@gmail.com> at 22:39 on Nov 14 in "[go-nuts] How can th..." ]
>> I have a couple of functions that look like this:
>>
>> func somefunc(saddr string) (FinalResultType, os.Error) {
>> if result, err := doSomeCalculation(); e != nil {
>> return nil, e
>> } else {
>> return wrapIt(result), nil
>> }
>> // return nil,nil
>> }
>>
>> Even though the third and last return (I believe) IS DEAD CODE, If I
>> don't include it (I left it commented as in the sample) I get the
>> following error:
>>
>> "function ends without a return statement"
>
> Its a compiler bug, make the function look like:
It's not a bug, it's a deliberate design decision.
-rob
This is a long standing request,
http://code.google.com/p/go/issues/detail?id=65
It's generally possible to work around the issue by removing the else clause and let the logic fall through to a return at the bottom of the function.
Sent from my iPad
Ah, I still thought it was considered a bug. So
issue 65 can be closed? (just asking)
grtz Miek
If you define the variables in the in the first line, you'll keep the
nice formatting along with the scope.
func somefunc(saddr string) (result FinalResultType, err os.Error) {
if result, err = doSomeCalculation(); err != nil {
return nil, err } return wrapIt(result), nil}
Regards
Josef