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
+48.5k Golang : Upload file from web browser to server
+44.8k Golang : Use wildcard patterns with filepath.Glob() example
+7.5k Golang : Rename part of filename
+9.2k Golang : Generate random Chinese, Japanese, Korean and other runes
+11.9k Golang : Convert(cast) bigint to string
+18.5k Golang : Generate thumbnails from images
+19.5k Golang : Example for DSA(Digital Signature Algorithm) package functions
+4.7k Unix/Linux : How to pipe/save output of a command to file?
+8.3k Golang : Convert word to its plural form example
+10.1k Golang : How to profile or log time spend on execution?
+7.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+11.6k Get form post value in Go