How to return multiple values from a function and assign them to variables in Golang or Go?
Tags: go
Published: 2022-08-13 18:57:39

---
  title: How to return multiple values from a function and assign them to variables in Golang or Go?
  published: true
  tags: go
  date: Sun Aug 14 2022 00:27:39 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/return-multiple-values-function-and-assign-to-variables-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-9a68019df567dace7165c4c7fd2faebc.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/return-multiple-values-function-and-assign-to-variables-golang)
  
To return multiple values from a function in Golang or Go, you can use the `return` keyword followed by the values you need to return separated by a `,` symbol (comma). Finally, you need to declare the types of the returning values inside the brackets after the function parameters brackets.

### TL;DR

```golang
package main

import (
    "fmt"
    "strings"
)

// getFirstAndLastName accepts a full name string
// and returns the first name and last name
// as the first and second return values
func getFirstAndLastName(fullname string) (string, string) {
    // splitting the fullname single string by whitespace
    names := strings.Split(fullname, " ")

    // return the `names` array first element as the first return value
    // and the second element as the second return value
    return names[0], names[1]
}

func main() {
    // call the `getFirstAndLastName` function
    // and assign the first return value to the `firstName` variable
    // and the second return value to the `lastName` variable
    // using the `:=` (assignment) operator
    firstName, lastName := getFirstAndLastName("John Doe")

    // log it to the console
    fmt.Println(firstName, lastName) // John Doe
}
```

For example, let's say we have a function that accepts a fullname of a user as a single string and returns the first name and the last name as separate return values.

To do that first let's create a function called `getFirstAndLastname` like this,

```golang
package main

func getFirstAndLastName(fullname string) (string, string) {
    // cool code here
}

func main(){

}
```

**NOTE: To know more about functions in Go see the blog on [How to create a function in Golang or Go?](https://melvingeorge.me/blog/create-function-golang-go)**

Now inside the function body, we can write the logic for separating the first name and the last name from the fullname.

The logic is to divide the full name string by whitespace. To do that we can use the `Split()` method from the `strings` standard package.

It can be done like this,

```golang
package main

// getFirstAndLastName accepts a full name string
// and returns the first name and last name
// as the first and second return values
func getFirstAndLastName(fullname string) (string, string) {
    // splitting the fullname single string by whitespace
    names := strings.Split(fullname, " ")
}

func main(){

}
```

As you can see code in the function body, we are splitting the full name single string whenever we encounter whitespace.

The `names` variable will be an array containing the split strings, such that we can assume that the first value in the `names` array is the first name and the second value is the last name for simplicity.

Now let's return the `names` array's first element as the first return value and the second element as the second return value separated by the `,` symbol (comma).

It can be done like this,

```golang
package main

// getFirstAndLastName accepts a full name string
// and returns the first name and last name
// as the first and second return values
func getFirstAndLastName(fullname string) (string, string) {
    // splitting the fullname single string by whitespace
    names := strings.Split(fullname, " ")

    // return the `names` array first element as the first return value
    // and the second element as the second return value
    return names[0], names[1]
}

func main(){

}
```

Now in the `main` function let's call the function and to get the return values from the `getFirstAndLastName` function, we can assign 2 variables called `firstName` and `lastName` separated by the `,` symbol and then use the `:=` operator to assign the return values from the function respectively.

It can be done like this,

```golang
package main

// getFirstAndLastName accepts a full name string
// and returns the first name and last name
// as the first and second return values
func getFirstAndLastName(fullname string) (string, string) {
    // splitting the fullname single string by whitespace
    names := strings.Split(fullname, " ")

    // return the `names` array first element as the first return value
    // and the second element as the second return value
    return names[0], names[1]
}

func main(){
    // call the `getFirstAndLastName` function
    // and assign the first return value to the `firstName` variable
    // and the second return value to the `lastName` variable
    // using the `:=` (assignment) operator
    firstName, lastName := getFirstAndLastName("John Doe")
}
```

Finally, let's console the `firstName` and the `lastName` variable to the console using the `Println()` method from the `fmt` standard package like this,

```golang
package main

import (
    "fmt"
    "strings"
)

// getFirstAndLastName accepts a full name string
// and returns the first name and last name
// as the first and second return values
func getFirstAndLastName(fullname string) (string, string) {
    // splitting the fullname single string by whitespace
    names := strings.Split(fullname, " ")

    // return the `names` array first element as the first return value
    // and the second element as the second return value
    return names[0], names[1]
}

func main() {
    // call the `getFirstAndLastName` function
    // and assign the first return value to the `firstName` variable
    // and the second return value to the `lastName` variable
    // using the `:=` (assignment) operator
    firstName, lastName := getFirstAndLastName("John Doe")

    // log it to the console
    fmt.Println(firstName, lastName) // John Doe
}
```

We have successfully returned multiple values and assigned them to 2 variables in the Go. Yay 🥳!

See the above code live in [The Go Playground](https://go.dev/play/p/lh_vNedJXA3).

That's all 😃!

### Feel free to share if you found this useful 😃.

---