Wednesday, January 13, 2021

Go (golang) Multiple Return Values - Different from C# Tuples

One of the features that I like in the Go programming language (golang) is that functions can return multiple values. In C#, we can mimic something similar using tuples, but they are not quite the same.
Go functions can return multiple values.
Let's take a look at how this works and how it differs from what is available in C#.

Motivation: I have been using C# as a primary language for 15 years. Exploring other languages gives us insight into different ways of handling programming tasks. Even though Go (golang) is not my production language, I've found several features and conventions to be interesting. By looking at different paradigms, we can grow as programmers and become more effective in our primary language.

Resources: For links to a video walkthrough, CodeTour, and additional articles, see A Tour of Go for the C# Developer.

Multiple Return Values

In a prior article, we took a look at deferring operations (Go (golang) defer - A Better finally). Part of that included an example of a function that returns multiple values. Let's start with the same function from that article.

The function is called "getPerson", and it fetches data from a web service. You can see this function on GitHub in the "CodeTour: Go for the C# Developer" repository: https://github.com/jeremybytes/go-for-csharp-dev/blob/main/async/main.go (starting on line 27).


This function returns multiple values, and inside the body, it calls a function that returns multiple values.

Declaration
Here's the function declaration:

Go
    func getPerson(id int) (person, error) {
       ...
    }

The declaration starts with "func" followed by the named of the function ("getPerson").

Next is the parameter set (enclosed in parentheses). In this case, we have a parameter named "id" which is an integer.

Next, we have the return types (also enclosed in parentheses). This indicates that this function returns 2 values: one of type "person" (a struct) and one of type "error" (a built-in type).

Note: There are several ways for declaring return types. If we only have a single return type, we do not need the parentheses. In addition, we can name the return values (similar to how the parameters are named). For more information, take a look at the "Function declarations" steps in the Code Tour (available on GitHub: https://github.com/jeremybytes/go-for-csharp-dev).

Returning Multiple Values
There are several code paths that return multiple values. In each case either the "person" or the "error" is populated (but not both). This follows the error handling philosophy in Go. For more information, see the previous article: Go (golang) Error Handling - A Different Philosophy.

Here is an error path that has a return:

Go
    if err != nil {
      return person{}, fmt.Errorf("error fetching person: %v", err)
    }

This shows a "return" statement that returns 2 values (delimited by a comma). The first value ("person{}") is an empty struct. "person" is a struct that contains multiple data fields. Structs themselves cannot be nil (null in C#), so we use an empty struct here. In an empty struct, all of the fields have default values (0 for integers, "" for strings, etc.).

The second return value is an "error". For more information on error handling, see the article mentioned above.

As another example, let's look at the "happy path" -- the last few lines of the function.

Go
    var p person
    err = json.NewDecoder(resp.Body).Decode(&p)
    ... // skipping the error handling
    return p, nil

In this code, "p" is a variable of type "person". The "Decode" function parses a JSON result and populates the "p" variable.

The final return statement returns the variable "p" for the "person" and "nil" for the "error".

So we return multiple values from a function by separating the values with commas.

Using Multiple Return Values

This shows how we can declare a function that returns multiple values, but how do we use those values? We can see an example of this in the first 2 lines of our function:

Go
    url := fmt.Sprintf("http://localhost:9874/people/%d", id)
    resp, err := http.Get(url)

The first line creates a URL for a web service call.

Note: the ":=" operator both creates a variable and assigns a value to it. The type of the variable is inferred based on the assignment (similar to a "var" in C#). Here is the equivalent of the first line in C#:

C#
    var url = string.Format("http://localhost:9874/people/{0}", id);

The next line of our Go function is where things get more interesting.

Go
    resp, err := http.Get(url)

"http.Get()" sends a web request and returns 2 values: a pointer to a response (*Response) and an error (error). To capture both of these values, we can use the ":=" operator to create and assign variables. Since we have multiple return values, we have multiple variables on the left of the ":=" operator.

The end result is that "resp" contains the "*Response" value and "err" contains the "error" value.

Using the getPerson Function
To relate declaring and using a function with multiple return values, let's go back to the "getPerson" function. Here's the declaration again:

Go
    func getPerson(id int) (person, error) {
       ... // most of the function
       return p, nil
    }

This function is used later in the sample code. (You can find this on line 72 of the file in the GitHub repository: https://github.com/jeremybytes/go-for-csharp-dev/blob/main/async/main.go.)

Go
    p, err := getPerson(id)

This calls the "getPerson" function and captures the "person" and "error" values in variables named "p" and "err", respectively.

Different from Tuples

We can mimic something similar to this in C# by using tuples, but tuples are a bit different. To see the difference, let's look at a similar function declaration in C#.

Go
    func getPerson(id int) (person, error) {
       ... // most of the function
       return p, nil
    }

C#
    static (Person, Error) GetPerson(int id)
    {
      ... // most of the function
      return (p, err);
    }

The C# version looks fairly similar to the Go version. The difference is that the Go function returns 2 separate values; the C# function returns a tuple -- a single item that contains multiple values.

Looks the Same...
We can mimic the different ways to get values from the function:

Go
    p, err := getPerson(id)

C#
    var (p, err) = GetPerson(id);

In C#, this creates 2 variables named "p" and "err" that are "Person" and "Error" types, respectively. So this looks pretty much the same as Go.

We can even discard values that we do not want to use:

Go
    p, _ := getPerson(id)

C#
    (var p, _) = GetPerson(id);

In both of these cases, the "error" return value is discarded.

...But Ultimately a Tuple is Different
Even though these look the same, there is something we can do in C# that we cannot do in Go.

C#
    var result = GetPerson(id);

In this case, "result" is a tuple that has the type "(Person, Error)". So a tuple is really a single "thing" that contains multiple elements. In Go, the return values are completely separate.

Tuples are interesting in C# because they let us put multiple values together as a single entity. And C# keeps adding different ways to deconstruct tuples into the various elements. Tuples are becoming more important as time goes on.

Final Thoughts

I find multiple return values to be quite interesting. I really like how Go has baked it into the language. In C#, tuples feel a bit awkward to me -- probably because of the different ways they can be used and deconstructed. I like the clean way that Go lets us easily return multiple values.

This feature makes the error handling philosophy possible. And that's something I find really interesting. (Again, see the prior article for more information: "Go (golang) Error Handling -- A Different Philosophy".)

Along these same lines, F# supports a different construct: Discriminated Unions. This allows us to return a single value, but the type of the value can vary. In our example, we could have a discriminated union that returns a "person" or an "error". I'll leave further exploration about this up to the reader.

The great thing about exploring different languages is that they take different approaches to problems. Along the way, we can also find out the various pros and cons to each approach. Ultimately, this gives us more options in our toolbox, and that makes it easier to pick the right one for a particular scenario.

Keep exploring.

Happy Coding!

No comments:

Post a Comment