I find the provision of named returned variables in Go to be a useful feature, because it can avoid the separate declaration of a variable or variables. However, in some instances I want to return a different variable to the variable declared in the function as the return-variable. That appears to work ok, however I do find it a little strange to declare a return variable and then return something else.
While writing a test program to help learn Go (not the one below), I found it a little annoying specifying the return variable in the return statement of functions returning multiple variables. Particularly so, because the variables had been named in the function declaration. I now find while posting this, that it appears that where there are named return variables, they don't need to be used in the return statement, just "return" will suffice and will implicitly use the named variables. That I find this a great feature.
So, although I have possibly partly answered my own question, could someone advise if my usage below is acceptable? I'm sure this is documented, but I haven't come across it, and it doesn't appear to be in the reference-book that I purchased which I think overlooks this feature.
Basically, the rule appears to be (as far as I can determine), that where named return variables are used, that the function statement declares the variables, and also the function can optionally implicitly uses them as the return values, however this can be overridden by using explicit return values.
Example Program :
package main
func main() {
var sVar1, sVar2 string
println("Test Function return-values")
sVar1, sVar2 = fGetVal(1)
println("This was returned for '1' : " + sVar1 + ", " + sVar2)
sVar1, sVar2 = fGetVal(2)
println("This was returned for '2' : " + sVar1 + ", " + sVar2)
}
func fGetVal(iSeln int) (sReturn1 string, sReturn2 string) {
sReturn1 = "This is 'sReturn1'"
sReturn2 = "This is 'sReturn2'"
switch iSeln {
case 1 : return
default : return "This is not 'sReturn1'", "This is not 'sReturn2'"
}
}