Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Function Returning Multiple Values in Go Language
Next article icon

Function Returning Multiple Values in Go Language

Last Updated : 01 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
In Go language, you are allowed to return multiple values from a function, using the return statement. Or in other words, in function, a single return statement can return multiple values. The type of the return values is similar to the type of the parameter defined in the parameter list. Syntax:
  func function_name(parameter_list)(return_type_list){       // code...  }  
Here,
  • function_name: It is the name of the function.
  • parameter-list: It contains the name and the type of the function parameters.
  • return_type_list: It is optional and it contains the types of the values that function returns. If you are using return_type in your function, then it is necessary to use a return statement in your function.
Example: C
// Go program to illustrate how a // function return multiple values package main  import "fmt"  // myfunc return 3 values of int type func myfunc(p, q int)(int, int, int ){     return p - q, p * q, p + q  }  // Main Method func main() {          // The return values are assigned into      // three different variables    var myvar1, myvar2, myvar3 = myfunc(4, 2)        // Display the values    fmt.Printf("Result is: %d", myvar1 )    fmt.Printf("\nResult is: %d", myvar2)    fmt.Printf("\nResult is: %d", myvar3) } 
Output:
  Result is: 2  Result is: 8  Result is: 6  

Giving Name to the Return Values

In Go language, you are allowed to provide names to the return values. And you can also use those variable names in your code. It is not necessary to write these names with a return statement because the Go compiler will automatically understand that these variables have to dispatch back. And this type of return is known as the bare return. The bare return reduces the duplication in your program. Syntax:
  func function_name(para1, para2 int)(name1 int, name2 int){      // code...  }    or    func function_name(para1, para2 int)(name1, name2 int){     // code...  }  
Here, name1 and name2 is the name of the return value and para1 and para2 are the parameters of the function. Example: C
// Go program to illustrate how to // give names to the return values package main  import "fmt"  // myfunc return 2 values of int type // here, the return value name  // is rectangle and square func myfunc(p, q int)( rectangle int, square int ){     rectangle = p*q     square = p*p     return   }  func main() {          // The return values are assigned into      // two different variables    var area1, area2 = myfunc(2, 4)        // Display the values    fmt.Printf("Area of the rectangle is: %d", area1 )    fmt.Printf("\nArea of the square is: %d", area2)     } 
Output:
  Area of the rectangle is: 8  Area of the square is: 4  

Next Article
Function Returning Multiple Values in Go Language

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Go-Functions
  • Golang

Similar Reads

    Naming the Return Values of a Function in Golang
    A return value helps to retain the final output of the function after it performs the instructions given in its body. Functions in Golang exhibit variety in return values and rely on the programmer to decide whether to name them or not. Golang introduces a concept of "Naked Return" allowing the use
    3 min read
    Functions in Go Language
    In Go, functions are blocks of code that perform specific tasks, which can be reused throughout the program to save memory, improve readability, and save time. Functions may or may not return a value to the caller.Example:Gopackage main import "fmt" // multiply() multiplies two integers and returns
    3 min read
    Golang Program that Uses Multiple Return Values
    In Golang, we can return multiple values at a time from a single function. Multiple return values can be achieved by changing the return type of the function in the function signature. Syntax :  func value( ) ( int , int ) {  return 1 , 0 ;  }  The (int, int) in this function signature explains that
    2 min read
    reflect.ValueOf() Function in Golang with Examples
    Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.ValueOf() Function in Golang is used to get the new Value initialized to the concrete value stored in the interfa
    2 min read
    Anonymous function in Go Language
    An anonymous function is a function that doesn’t have a name. It is useful when you want to create an inline function. In Go, an anonymous function can also form a closure. An anonymous function is also known as a function literal.ExampleGopackage main import "fmt" func main() { // Anonymous functio
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences