3 ms·
> Actually, you can declare a fixed-length array as a type. So if you need to return a triple of ints, you can declare the return type to be [3]int and have the
by pcwalton 13y ago
> Actually, you can declare a fixed-length array as a type. So if you need to return a triple of ints, you can declare the return type to be [3]int and have the type system check that you're actually returning an array of ints of length 3.
That's different from a tuple as it is commonly known in Python and statically typed languages. Tuples can be heterogeneous in languages like Python, OCaml, and Haskell, which Go fixed length arrays can't be. For example, in Go you can't have `(int, float)`.
- dbaupp 13y agoDoes Go really not have tuples? (Aren't multiple return values tuples?)
- pcwalton 13y agoIt has multiple return values and syntax for assigning the results of a function that returns multiple values, but tuple values are not first class and the destructuring form is limited to calling a function and extracting its return values.
- khyryk 13y agoSomething like this? http://play.golang.org/p/6nvdLWOi5D http://play.golang.org/p/6nvdLWOi5D
- tikhonj 13y agoNot quite. What you have is a single type interface{} that happens to be the super type of all other types. You still haven't solved the issue of having multiple different types in a tuple--you've just thrown all the type information away! Think of a tuple as an anonymous struct. (Or perhaps think of a struct as a tuple with labels :P.) We want to statically differentiate between a pair of ints, a pair of strings and an int, string pair, which we simply can't do with interface{}.