Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return input variables in golang

Tags:

return

go

I just got started with Golang, and I saw the typical swap function example:

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

I automatically thought that the named returns could have solved it and that it was a sweeter example, so I tried the shorter version:

package main

import "fmt"

func swap(z, y int) (z, y int) {
    return
}

func main() {
    fmt.Println(swap(2, 3))
}

But to by my surprise it didn't compile complaining about a duplicate argument. Why is not possible to return an input argument? Am I doing something wrong or it is just not supported?

I thought this was a totally valid use case and that it could have been many other examples for this usage.

like image 678
Víctor R. Escobar Avatar asked Oct 08 '17 20:10

Víctor R. Escobar


People also ask

How do you return variables in Golang?

To declare the named result or return parameters, just use the return type part of the function signature. Below is the general syntax to declare a function in Golang. Syntax to declare a function without named return arguments: func function_name(Parameter-list)(Return_type){ // function body..... }

How do I return an integer in Golang?

In this example, the add() function takes input of two integer numbers and returns an integer value with a name of total. Note the return statement is required when a return value is declared as part of the function's signature.

How can I return two values from a function in Golang?

In Golang, we can return multiple values at a time from a single function. Multiple return values can be achieved by changing the return type of the function in the function signature. The (int, int) in this function signature explains that the return type is two integers.

How do you return a value from a function in Golang?

Simple function with return value in Golang In this example, the add () function takes input of two integer numbers and returns an integer value with a name of total. Note the return statement is required when a return value is declared as part of the function's signature. The types of input and return value must match with function signature.

How to use variables in Golang?

Now, we will see how you can use variables in GoLang. Since Go is a statically typed programming language, that means the variable type is inferred before compilation. The var keyword is used to declare a variable. The code below shows how the variable declaration is done in Go.

How to take input from the user in Golang?

Scanln function can be used to take the input from the user in the Golang. Below is the example of taking input from the user: Now save this file and execute as shown in the below screenshot: main Package: When we build reusable pieces of code, we will develop a package as a shared library.

What are the return or result parameters of a Go function?

The return or result "parameters" of a Go function can be given names and used as regular variables, just like the incoming parameters.


Video Answer


2 Answers

I'm also a Golang beginner. Here's what I managed to find out.

The problem is essentially, that you declare two variables named z, then expect them to be unified. This is not supported, and in fact would go against the main goal of named return types, which is to document the meaning of the values returned.

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
}

A variable is declared twice, and this is confusing for Go. If we look at what the 'tour of go' has to say about named return values, we can see the issue. It's not the greatest source, but it's a source nonetheless:

Go's return values may be named. If so, they are treated as variables defined at the top of the function.

That is to say, your example is almost exactly like badFunction. To the compiler, it looks a bit 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 a related though admittedly not equal error. The error message you receive there appears to essentially be a pre-check to prevent the user from seeing the code produced when desugared.


As this Stackoverflow question reports, named return values should essentially be for documentation only. However, it does mention the possibility of accidental shadowing. It may be that an earlier Go version supported this, but has since been changed to prevent bugs due to this kind of name collision, however I have not found anything pertaining to this.

The effective go section on the topic also has something to say:

The return or result "parameters" of a Go function can be given names and used as regular variables, just like the incoming parameters. When named, they are initialized to the zero values for their types when the function begins; if the function executes a return statement with no arguments, the current values of the result parameters are used as the returned values.

The names are not mandatory but they can make code shorter and clearer: they're documentation.


TL;DR: The compiler doesn't unify names in the way you might expect. This kind of implicit shadowing not supported, and should be actively avoided to prevent certain easily avoidable bugs.

like image 150
AJF Avatar answered Nov 15 '22 07:11

AJF


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

like image 34
Eugene Lisitsky Avatar answered Nov 15 '22 09:11

Eugene Lisitsky