func return with if... else...

164 views
Skip to first unread message

Eric Brown

unread,
Jan 14, 2017, 4:31:32 PM1/14/17
to golang-nuts
Using go, when I create a function with a return... and that function uses an if... else... condition w/ the return being passed under each, the compiler still throws an error 'missing return at end of function'?  I can put a return at the end of the function, but it will never get to that point because of this condition.  Is that expected, or could I be doing something different to prevent this outcome?  Not that it matters... but I hate just having random returns at the bottom of functions that don't do anything other than satisfy this issue.

Paul Jolly

unread,
Jan 14, 2017, 4:37:12 PM1/14/17
to Eric Brown, golang-nuts
The compiler does understand the exhaustiveness of if ... else ... : https://play.golang.org/p/PAcg9oUAxA

Can you post some code to highlight the problem you're having? 

On 14 January 2017 at 21:31, Eric Brown <edb...@gmail.com> wrote:
Using go, when I create a function with a return... and that function uses an if... else... condition w/ the return being passed under each, the compiler still throws an error 'missing return at end of function'?  I can put a return at the end of the function, but it will never get to that point because of this condition.  Is that expected, or could I be doing something different to prevent this outcome?  Not that it matters... but I hate just having random returns at the bottom of functions that don't do anything other than satisfy this issue.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Brown

unread,
Jan 14, 2017, 4:37:17 PM1/14/17
to golang-nuts
Sorry guys, found the issue... and it's from my inexperience.  The functions that is happening to are ones I used a switch condition (which I could've used an if condition on instead).

I used:

switch strings.Contains(targetDatabase.Driver, ConvertString("[!]sqlite[!]")) {
case true:
case false:

instead of

if strings.Contains(targetDatabase.Driver, ConvertString("[!]sqlite[!]")) {
} else {
}

Of course the compiler won't always assume all possible outcomes of a switch to ensure it's being caught... and thus the problem.  Wish I wouldn't have pre-emptively posted this now.  My apologies...

James Bardin

unread,
Jan 14, 2017, 4:39:20 PM1/14/17
to golang-nuts
If you're "if" block returns, and there's nothing after the "else" block, then you don't need the "else" at all.

Justin Israel

unread,
Jan 14, 2017, 4:42:33 PM1/14/17
to Eric Brown, golang-nuts
On Sun, Jan 15, 2017 at 10:37 AM Eric Brown <edb...@gmail.com> wrote:
Sorry guys, found the issue... and it's from my inexperience.  The functions that is happening to are ones I used a switch condition (which I could've used an if condition on instead).

I used:

switch strings.Contains(targetDatabase.Driver, ConvertString("[!]sqlite[!]")) {
case true:
case false:

instead of

if strings.Contains(targetDatabase.Driver, ConvertString("[!]sqlite[!]")) {
} else {
}

Of course the compiler won't always assume all possible outcomes of a switch to ensure it's being caught... and thus the problem.  Wish I wouldn't have pre-emptively posted this now.  My apologies...

Having a default: case that returns might have solved that issue as well.
 


On Saturday, January 14, 2017 at 3:31:32 PM UTC-6, Eric Brown wrote:
Using go, when I create a function with a return... and that function uses an if... else... condition w/ the return being passed under each, the compiler still throws an error 'missing return at end of function'?  I can put a return at the end of the function, but it will never get to that point because of this condition.  Is that expected, or could I be doing something different to prevent this outcome?  Not that it matters... but I hate just having random returns at the bottom of functions that don't do anything other than satisfy this issue.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Eric Brown

unread,
Jan 14, 2017, 5:30:50 PM1/14/17
to golang-nuts
If you have a catch-all, is it better to use after an if condition without an else, or put it in an else:

if condition {
   return A
}

return B

or:

if condition {
   return A
} else {
   return B
}

Just curious is there is a prefered standard to this for readability, or if it's just to each their own...


On Saturday, January 14, 2017 at 3:31:32 PM UTC-6, Eric Brown wrote:

Dave Cheney

unread,
Jan 14, 2017, 5:44:33 PM1/14/17
to golang-nuts
The former.

Eric Brown

unread,
Jan 14, 2017, 5:47:42 PM1/14/17
to golang-nuts
Thank you...

On Saturday, January 14, 2017 at 4:44:33 PM UTC-6, Dave Cheney wrote:
The former.
Reply all
Reply to author
Forward
0 new messages