Multiple Return Values: Golang

Avatar of Rakesh Mothukuri

Rakesh Mothukuri

| Reading Time : 1 minute

Avatar of Rakesh Mothukuri Avatar of Rakesh Mothukuri Avatar of Rakesh Mothukuri Avatar of Rakesh Mothukuri

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.


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

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.