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
+5.3k Golang : Get FX sentiment from website example
+6.6k Golang : How to determine if request or crawl is from Google robots
+6.9k Golang : Decode XML data from RSS feed
+6.1k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+19.8k Golang : Append content to a file
+9.1k Golang : Serving HTTP and Websocket from different ports in a program example
+4.9k Javascript : How to get width and height of a div?
+5.1k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+18.4k Golang : How to remove certain lines from a file
+15.5k Golang : invalid character ',' looking for beginning of value
+14.5k Golang : Rename directory
+10.2k Golang : Use regular expression to get all upper case or lower case characters example