Software Development

Go – Return multiple values from a Function

In Go (or Golang), it is possible to return more than one value from a function.

Function Declaration

I’ve never seen or used something like it before but I think the multi-value-return is great. Most programming language has similar facility that support only single return value.

The general syntax of functions is Go is as follows.

[wp_ad_camp_1]

We have the func keyword that indicates MyFunction is a function. It has a list of parameters, and a list of result values.

Go Codes

The codes below demonstrates his built-in facility.  Here we have the GetDomainDetails function that returns two (2) values of string type.

The use of “:=” is meant for locally-scoped variables, e.g., function local, variables.

[wp_ad_camp_2]

The order of the variables that receive the values matter.

It outputs

Download the codes

[wp_ad_camp_3]

https://github.com/Turreta/Golang/tree/master/Return%20multiple%20values%20from%20function

You Might Also Like