I just read that map[Type]interface{} specifies the mapping of keys of Type type, and its value is any, that is interface{}.
Isn't this almost the same as defining a structure, that is, type Name struct{ key1; value1, ...., keyn: valuen}?
What is the difference between these two types of mapping?
I read https://www.digitalocean.com/community/tutorials/how-to-use-json-in-go but I still don't understand the difference.
Or what is the difference between map[type] interface{} which we define in a more general way?
We define each key-value pair through the structure?
Are these two methods just defining objects composed of key-value pairs?
Structure and mapping are different data structures. They have many differences. Here are just a few:
A structure has a fixed number of fields, which are declared once and cannot be changed.
3a15cefd8a1cc7ac8a7f27a0d3f9b13The map can grow or shrink at runtime.
vector := map[string]float64{ "x": 2.0, "y": 2.0, } vector["z"] = 2.0
You can loop through map entries.
for key, val := range vector { fmt.Println(key, val) }
Structures do not support iteration (unless you use reflection).
Structure fields can have labels (additional attributes):
type User struct { Name string `json:"name"` Password string `json:"password"` }
The map does not have this function.
The above is the detailed content of What is the difference between mapped interface {} and type structure {}?. For more information, please follow other related articles on the PHP Chinese website!