10

I have List of Tuple in python as you see below:

data  = [
    (435347,'cat'),  
    (435347,'feline'),
    (435347,'lion'),
    (6765756,'dog'),
    (6765756,'hound'),
    (6765756,'puppy'),
    (435347,'kitten'),
    (987977,'frog')
    ]

how can i convert data into GO equivalent, I have googled it but everywhere they have specified with the same data-type of multidimensional array like this,

int_list := [4][2]int{{1, 0}, {2, 0}, {3, 0}, {4, 0}}

string_list := make([][]string, 0)

a := [1][2]string{{"lion","dog"}}

help me out..!

Updated:

for Mixed-type Collections of Array:

array := []interface{}{1, "dog", "lion", 12, 45.09, true, false}

2 Answers 2

12

There are two possible solutions here: actual multidimensional arrays of mixed data types (what you seem to be asking for) and arrays of structs (what you probably want).

Multidimensional arrays of mixed data-types using interfaces

If you really want to do multi-dimensional arrays of mixed types, you need to use interfaces:

interfaceArray := [2][2]interface{}{{1,"dog"},{2,"cat"}}
fmt.Println(interfaceArray)

Outputs:

[[1 dog] [2 cat]]

Interfaces must be handled with care as they can be... anything. This means that there is no enforcement that the first argument is an int and the second is a string. I can just as easily add {"lion", 3} and the compiler will be happy, but your program will crash if it doesn't check the type first.

You can do type conversions and assertions on interfaces to ensure the correct data types.

Arrays of structured data

Putting python tuples in a list does not make it a multi-dimensional array. The closest equivalent in Go would be an array of structs.

type animal struct {
  id int
  name string
}

animalList := [2]animal{{1, "cat"}, {2, "dog"}}
fmt.Println(animalList)

Outputs:

[{1 cat} {2 dog}]

In this case, the types are clearly defined, and I cannot store {"lion", 3} in the array.

final note: [size]type defines an array. []type defines a slice. I'm using arrays here as this is what you mention in your question, but slices would be just as (or probably more) appropriate.

Sign up to request clarification or add additional context in comments.

2 Comments

so there is no way to declare a multi-dimensional array of different datatype without using interface..? Why like this..?
There's almost never a way to do mixed-type multi-dimensional arrays without using something more generic (eg: a void * in C) that can represent anything. python doesn't have them either as tuples are not arrays, so you really have a list of tuples, not a multi-dimensional array.
8

You can't create multi dimensional array with two different type. Better to use structure. Hope this will work for your purpose.

package main

import "fmt"

type Data struct {
    i int
    s string
}

func main() {
    data := []Data{
        {435347,"cat"},
        {435347,"feline"},
        {435347,"lion"},
    }

    fmt.Println(data)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.