I have the following function:
func fitrange(a, x, b int) int {
if a > b {
a, b = b, a
}
switch true {
case x < a:
return a
case x > b:
return b
default:
return x
}
}
The go compiler complains that the "function ends without a return statement" even though every possible path through the switch
statement returns a value. Is there any way to get around this other than adding a dummy return
statement at the end of the function?
Remove the default
case all together and return x
after the switch.
Like:
func fitrange(a, x, b int) int {
if a > b {
a, b = b, a
}
switch true {
case x < a:
return a
case x > b:
return b
}
return x
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With