Multiple Variable Return in Function
A function can return any number of results in Golang. Consider the following program as an example.
package main
import "fmt"
func swap(x string, y int) (int, string) {
return y, x
}
func main() {
a, b := swap("Hello", 1)
fmt.Println(a, b)
}
Output: 1 Hello
In the above program you are passing one string and one int from main function to swap() function. And swap function will return two different datatype’s variable at a time. This is so amazing. Even JAVA can not do it that simple. If you want to return multiple values in JAVA, then you have to encapsulate them into a class and then return an object of that class.