MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go Function Return values

In Go, a function can be used to return 1 or more values. To return a value from a function we need to set a return type after closing parentheses and have a return keyword with the value.

Go Function return value syntax


func FunctionName(param1 type, param2 type, param3 type) type {
  // set of code to be executed
  return output
}


Go Function Return Example

Below is an example of Integer return type

package main
import ("fmt")

func do_plus_numbers(x int, y int) int {
  return x + y
}

func main() {
  fmt.Println(do_plus_numbers(1, 2))
}

Output

3


Go Function another Return Example

Below is an example of a String return type

package main
import ("fmt")

func say_hello_person(name string) string{
  return "hello " + name
}

func main() {
  fmt.Println(say_hello_person("Jhon"))
}

Output

hello Jhon


Go Function Multiple Return values

In Go, you can return multiple values from a function by specifying type for each value

package main
import ("fmt")

func do_plus_and_minus_numbers(x int, y int) (plus_value int, minus_value int) {
  plus_value = x + y
  minus_value = x - y
  return
}

func main() {
  fmt.Println(do_plus_and_minus_numbers(3, 2))
}

Output

5 1


Go Function Multiple Return values different types

In Go, you can return multiple values from a function by specifying type for each value

package main
import ("fmt")

func do_plus_and_minus_numbers(x int, name string) (plus_value int, say_hi_to_name string) {
  plus_value = x + x
  say_hi_to_name = "hi " + name 
  return
}

func main() {
  fmt.Println(do_plus_and_minus_numbers(3, "Jhon"))
}

Output

6 hi Jhon

Conclusion

In this page (written and validated by ) you learned about Go Function Return values . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Function Recursion.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.