Golang return variables

I just started working with Golang and I saw a typical example of a swap function:

func swap(x, y string) (string, string) { return y, x } 

I automatically thought that named returns could solve this and that it was a sweeter example, so I tried a shorter version:

 package main import "fmt" func swap(z, y int) (z, y int) { return } func main() { fmt.Println(swap(2, 3)) } 

But to my surprise, he did not compile complaints about the duplicate argument. Why is it impossible to return an input argument? Am I doing something wrong or just not supported?

I thought this was a perfectly acceptable use case and that there could be many other examples for this use.

+5
source share
2 answers

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.

+4
source

I think the problem is not in returning the input argument, but in duplicating the names: y and z are declared twice at the same level, and the compiler cannot distinguish.

+2
source

Source: https://habr.com/ru/post/1272441/


All Articles