⚠️ This lesson is retired and might contain outdated information.

Returning Multiple Values from a Function in Go

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 2 years ago

Returning multiple values from a function is a common idiom in Go, most often used for returning values along with potential errors. We'll go over how to return multiple values from a function and use those values in our program.

Instructor: [00:00] Here we have a Go program that uses a function that takes a number, multiplies it by two, and then returns that number. If we run this program, we can see the output is eight because we passed in four to the function.

[00:14] To make this function return both n and the n2 value, we'll first need to change the return type to return two ints. Then, we'll use a comma to return both n and n2 in our values. When we assign the result of *2 to our variables, we will also want to use a comma to capture both variables.

[00:44] Then, we can print both of them at the same time. Let's run the program again, and now we can see four and eight.

egghead
egghead
~ 11 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today