12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
11 : 17 : 29 : 10
Join egghead, unlock knowledge.

Want more egghead? It's 58% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 40% Off
Become a member
to unlock all features

    Returning Multiple Values from a Function in Go

    Chris BiscardiChris Biscardi

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: 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.

    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.

    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.