I am also a beginner golang. Here is what I managed to find out.
The problem is that you declare two variables named z and then expect them to be unified. This is not supported and will actually run counter to the main purpose of named return types, which is to document the value of the returned values.
To explain in more detail, this is a bit like writing the following code:
func badFunction(a int) int { var a int = 0 return a }
The variable is declared twice, and this is confusing for Go. If we look at what 'tour of go' has to say about named return values, we can see this problem. This is not the biggest source, but nonetheless it is the source:
Return values Go can be named. If so, they are treated as variables defined at the top of the function.
That is, your example is almost exactly like badFunction . For the compiler, it looks something like this:
func swap(a, b int) (int, int) { var a int = 0 var b int = 0 return b, a }
Naturally, the compiler complains about a redeclared in block , which is connected, although admittedly, and is not an equal error. The error message that you get there seems to be a preliminary check so that the user cannot see the code created when it was released.
As this question is with a question about static threads , the names of the returned values should be mainly for documentation. However, it mentions the possibility of accidental shading. Perhaps an earlier version of Go supported this, but has since been modified to prevent errors due to this kind of name conflict, however I have not found anything in this regard.
an effective go section on the topic also has something to say:
The return or end "parameters" of the Go function can be given names and used as regular variables, as well as incoming parameters. When the name, they are initialized with zero values for their type, when the function starts, if the function executes the return statement without arguments, the current values of the resulting parameters are used as return values.
Names are optional, but they can make the code shorter and clearer: this is the documentation.
TL DR: The compiler does not concatenate names as you might expect. This type of hidden shadowing is not supported and should be actively avoided to prevent some easily fixed errors.