Dictionary in Go

Basically the problem is that it’s hard to encounter a requirement to store values of different types in the same map instance in real code.

In your particular case, you should just use a struct type, like this:

type person struct {
  name string
  age  int
}

Initializing them is no harder than maps thanks to so-called “literals”:

joe := person{
  name: "Doe, John",
  age:  32,
}

Accessing individual fields is no harder than with a map:

joe["name"] // a map

versus

joe.name // a struct type

All in all, please consider reading an introductory book on Go
along with your attemps to solve problems with Go,
as you inevitably are trying to apply your working knowledge
of a dynamically-typed language to a strictly-typed one,
so you’re basically trying to write Python in Go, and that’s
counter-productive.

I’d recommend starting with
The Go Programming Language.

There are also free books on Go.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)