Home > database >  How might I create a map in Go with tuples as values?
How might I create a map in Go with tuples as values?

Time:02-24

How might I create a map that has strings as keys and tuples ,of one or many elements, as values? The elements of the tuples are to be strings of phone numbers. Much like what I have in the following Python code:

chicas = { "Alexia":("600000000"), 
"Paola":("600000008", "600000007", "600000005", "600000001", "600000004", "600000000"),
"Samanta":("600000009"),
"Wendy":("600000005")}

The variable chicas is meant to be immutable. I've started with:

type chica struct {
  name string
  number tuple
}

but I get from Go: undefined: tuple.

CodePudding user response:

If the size of your values are fixed, you can make a type like you have, or just use a map[string]string

Eg:

type MyTuple struct {
   key string
   value string
}

func main() {
  var x := make(map[string]MyTuple)
  x[“foo”] = MyTuple{ key: “bar”, value: “baz” }
}

Alternatively, you can do map[string][]string to make a map of strings to slices of strings, []MyTuple, or map[string]map[string]string to make a map of strings containing maps.

  • Related