Golang : Return multiple values from function
One of the features that I like about Golang is the ability to return multiple values from a function (Python and Perl can do that as well).
In this short tutorial, we will explore how to create a function that return multiple values.
To return more than one value, we just add another return parameter. For example, (int int)
means that the function is expected to return 2 integer values.
func returnTwoValues(a, b int) (int, int)
This code will demonstrate just that :
package main
import (
"fmt"
)
func returnTwoValues(a, b int) (int, int) {
return a + b, a - b
}
func main() {
a := 10
b := 8
aPlusB, aMinusB := returnTwoValues(a, b)
fmt.Printf("A is %d and B is %d \n", a, b)
fmt.Printf("A Plus B : %d and A Minus B : %d \n", aPlusB, aMinusB)
}
output :
A is 10 and B is 8
A Plus B : 18 and A Minus B : 2
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+4.6k JavaScript : Rounding number to decimal formats to display currency
+22.7k Golang : Set and Get HTTP request headers example
+5.2k Responsive Google Adsense
+14.8k Golang : Normalize unicode strings for comparison purpose
+19.8k Golang : Append content to a file
+30.4k Golang : How to verify uploaded file is image or allowed file types
+7.5k Android Studio : How to detect camera, activate and capture example
+11.4k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+29.4k Golang : Save map/struct to JSON or XML file
+12k Golang : Determine if time variables have same calendar day
+12k Golang : Clean formatting/indenting or pretty print JSON result