this doesn't compile.
------------------------------------------------------------
package main
import "fmt"
func f() {
fmt.Printf("f\n")
return
}
func g() {
fmt.Printf("g\n")
return f()
}
func main() {
g()
}
------------------------------------------------------------
Giving these errors
./void_func.go:12: f() used as value
./void_func.go:12: too many arguments to return
This idiom does work in a lot of languages (eg C and Python).
------------------------------------------------------------
#include <stdio.h>
void f(void) {
printf("f\n");
return;
}
void g(void) {
printf("g\n");
return f();
}
int main(void) {
g();
return 0;
}
------------------------------------------------------------
I suspect it works in C and Python because functions always return
something though that may be void/None whereas go functions really can
return nothing.
It would be neat if it could be made to work in Go. It means that if
you refactor functions it makes it much more regular, as in "return
f()" will always work regardless of the type signature of g().
Here is the actual example that set me off thinking about this
func Error500(w http.ResponseWriter, message string) {
w.WriteHeader(500)
fmt.Fprint(w, message)
}
func Request(w http.ResponseWriter, r *http.Request) {
// Stuff
if !somethingOk {
Error500(w, "Something not OK")
return
}
// Compute more stuff
if !somethingElseOk {
Error500(w, "Something else not OK")
return
}
// Stuff
}
The two calls to Error500 would look much neater like this in my
opinion as the intention of tail calling Error500 is expressed better.
func Request(w http.ResponseWriter, r *http.Request) {
if !somethingOk {
return Error500(w, "Something not OK")
}
// Compute more stuff
if !somethingElseOk {
return Error500(w, "Something else not OK")
}
// Stuff
}
--
Nick Craig-Wood <[email protected]> -- http://www.craig-wood.com/nick
--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.