Okay, so in this article, we’re going to talk about a few things. We’re going to look at how we return multiple values from a function; within that function we’re also going to take subsets of a slice.
Let’s go through all of the code below at a high level. We will then zoom in on the final function that returns multiple values. I have covered off the code in more detail in previous articles, so this will just be a refresher.
- Import the fmt library
- Create a new data type called deck, which is a slice of strings
- Define a function to create a new deck, which:
- Creates a new instance of the deck data type called cards
- Creates 2 slices which include all the possible suits and values of a deck of cards
- Loops through those suits and values & concatenates them together. For example value = Ace and suit = Diamonds, then the output is Ace of Diamonds
- Then a print function is defined, which loops through each item in our cards slice and prints them out
- Create a function called deal, which we are going to focus on.

The deal function takes two inputs. The first is the deck instance that we are working with and the other is the number of cards that we want to deal from the deck, which is input as an integer.
The output from the function is defined as (deck, deck), this means, we will return two decks as the outputs.
The first deck to output is a subset of the slice, starting from index zero and ending with the index set by num_cards variable. So if we are going to deal 3 cards from our deck, we will set it to d[:3]. This takes all values up to and not including index 3, so it will return indexes 0, 1 and 2 from the slide d.
The second deck it returns is a list of the cards that weren’t dealt. So, from index num_cards to the end of the slice. So, if we have a num_cards set to 3, we would get index 3, 4, 5 and so on as a result.
All of the above was happening in deck.go; if we flip back to main.go, we can see how these functions are called. In the main function, we have defined: dealt, remaining_cards := deal(cards, num_cards), here we’re passing num_cards and the cards deck into the deal function. We then assign the output of the function to the variables dealt and remaining_cards.

The output then would be:
