Multiple Return Values: Golang

| Reading Time : 1 minute | #backenddevelopment #Go


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.


Articles from blogs I follow around the net

powerctl: A small case study in Hare for systems programming

powerctl is a little weekend project I put together to provide a simple tool for managing power states on Linux. I had previously put my laptop into suspend with a basic “echo mem | doas tee /sys/power/state”, but this leaves a lot to be desired. I have to u…

via Drew DeVault's blog August 28, 2022

United States v. Microsoft Corp exhibits

Links to exhibits from the Microsoft anti-trust case, with a bit of info on each link. Projection of PC marketshare Share of new browser users Share of the browser market, grouped by major ISP, 3 month moving average Share of the browser market, grouped by ma…

via danluu.com August 24, 2022

Status update, August 2022

Hi all! This month I’ve been pondering offline-first apps. The online aspect of modern apps is an important feature for many use-cases: it enables collaboration between multiple people and seamless transition between devices (e.g. I often switch between my pe…

via emersion August 14, 2022

Generated by openring

© Copyright 2021-2022. Rakesh Mothukuri