package main
func test() int {
if true {
return 1
} else {
return 0
}
}
func main() {
print(test())
}
fails to compile:
8g -o test.8 test.go
test.go:7: function ends without a return statement
Is that by design?
Alex
Thanks for the pointer.
But I still not sure about how should my code look:
func test() int {
if true {
return 1
} else {
return 0
}
}
or
func test() int {
if true {
return 1
}
return 0
}
or
func test() int {
if true {
return 1
} else {
return 0
}
return 0 // just to shut up compiler
}
considering "if" branches are equally important.
What do you prefer?
Alex
> To unsubscribe from this group, send email to golang-nuts+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
>
On Mar 25, 4:30 pm, Andrew Gerrand <a...@google.com> wrote:
> The middle case, for sure. The less nesting, the easier it is to read.
>
Ok. Thank you.
Alex
if true {
result = 1;
}
return result;
}
---
It is easier to read, isn't it?
Paladin
Why don't you try to write your code like these?
---
func test() int {
result := 0;
if true {
result = 1;
}
return result;
}
---
It is easier to read, isn't it?
Paladin