Skip to main content

Multiple Return Values: Golang

·148 words·1 min·
Table of Contents
Go Web Dev - This article is part of a series.
Part : This Article

In Golang, It is allowed to return multiple values from a function, using a return statement. The type of the return values is matches type defined for parameters.

Example:

func functionName(x, y int)(int, int){
     return x*3, y*7
}

Multiple Return values

Named Return Values
#

It is also provided names to the return values and the same can be used in code. It is not necessary to write these names with a return statement because the Go compiler will automatically understand that these variables have to return. This type of return is known as the bare return. This reduces the duplication in your program.

func calculateArea(x, y int)( rectangleArea int, squareArea ) int {
     rectangleArea = x*y
     squareArea = x*x
     return
}

Here rectangleArea and squareArea are the named returned values.

Community feedback
#

Jonas B. Nielsen Thanks a ton for pointing my oversight failure. I’m glad you liked this article.

Go Web Dev - This article is part of a series.
Part : This Article