Is golang tuple possible? [SOLVED]

Can we create tuple in golang?

This article will show you some ways to use tuples in the Go programming language. In general, a tuple is a sequence of finite-ordered elements and in programming languages, a tuple means a pair or an ordered set of values. The values of a tuple can be separated by a comma or any other syntax, depending on the rules of specific programming languages. In essence, a tuple can store various types of data types and is immutable.

Unlike some other programming languages such as Python,...at the time of writing this article Go does not have a tuple type. Therefor, you cannot use the tuple type in your Go program, but you can emulate some of its functionality by following a set of rules.

Advertisement

 

Method-1: Using interface{} to create a tuple

We can use interface{} type to create a struct with different data types like one is a string and another is an integer type.

interface{}: An interface is two things:

  • it is a set of methods,
  • but it is also a type

The interface{} type (or any with Go 1.18+), the empty interface is the interface that has no methods. Since there is no implements keyword, all types implement at least zero methods, and satisfying an interface is done automatically, all types satisfy the empty interface. That means that if you write a function that takes an interface{} value as a parameter, you can supply that function with any value.

Here is an example of how to use interface{} to simulate a tuple.

package main

import "fmt"

type Book struct {
	title, price interface{}
}

func main() {
	book1 := Book{"Harry Potter 1", 15.7}
	book2 := Book{"Lord of the ring", 17.1}
	book3 := Book{"Gone girl", 24.8}
	fmt.Println("Book 1 Info :", book1, "Book 2 Info :", book2, "Book 3 Info : ", book3)
	fmt.Println("Book 3 price :", book3.price)
	fmt.Println("Book 1 Title :", book1.title)
}

Output:

$ go run main.go
Book 1 Info : {Harry Potter 1 15.7} Book 2 Info : {Lord of the ring 17.1} Book 3 Info :  {Gone girl 24.8}
Book 3 price : 24.8
Book 1 Title : Harry Potter 1

Explanation:

Advertisement

The output shows that we can print data as a pair and also access a single property from the data using a dot. Although these features are comparable to a tuple, this is not a tuple.

You can also return multiple values in a tuple. Though the Go lacks a tuple type, you can use it with a function that returns multiple values. Consider the following code example:

package main

import "fmt"

func returnMultipleValues() (string, float64) {
	return "Harry Potter", 15.7
}

func main() {

	title, price := returnMultipleValues()
	fmt.Println("Title :", title)
	fmt.Println("Price :", price)
}

Output:

$ go run main.go
Title : Harry Potter
Price : 15.7

 

Method-2: Using Go generic to create a tuple

Recently, Go 1.18 beta was released with the generics feature. This will make it very easy to declare tuple types:

package main

import (
	"fmt"
	"time"
)

func main() {
	//create a channel
	c := make(chan Pair[string, int])

	//create a pair
	foo := Pair[string, int]{"hello", 10}

	go printChannelData(c)

	//send pair to chanel
	c <- foo

	time.Sleep(1 * time.Second)
	fmt.Println("End main")
}

type Pair[T, U any] struct {
	First  T
	Second U
}

// print all the received data
func printChannelData(c chan Pair[string, int]) {
	fmt.Println("Data received")
	fmt.Println("Data in channel is: ", <-c)
}

Output:

$ go run main.go
Data received
Data in channel is:  {hello 10}
End main

 

Summary

These are some approaches and by following these you may apply a tuples characteristics in the Go language. It’s important to note that Go 1.18 is still in beta, and specifically, the generics feature is still in its very early stages. I hope that in the future they will add better support for the tuple use case.

 

References

https://github.com/barweiss/go-tuple
https://go.dev/blog/go1.18beta1
Pair/tuple data type in Go - stackoverflow.com

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X