Using multidimensional map in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

http://tnt.wicast.tk/2015/11/02/golang-multiple-dimension-map/

Golang's Xml/json Analytic library is easy to use at first glance, as long as a struct of the same structure can be imported into the variable at once. In fact, the structure of the struct is very prone to structural deviations, and the most deadly is the Unmarshal () The execution of the time is not strictly imported so there is no error.

So these two days wrote a to Golang with the XML to Struct generator, hope to be once and for all.

But in the production process encountered a not too easy to find the pit ... That's what the title says about multidimensional map.

What is map

The first map in Golang is not the same as the map function in Js,py. The former is an underlying data type in Golang, with key:value characteristics, while the latter map is the map in MapReduce that distributes a set of data elsewhere.

Use map, and a multi-dimensional map of the pit

The bottom of the map is a hash, usually I like to use this to give a group of data to remove duplication. When using map, you need to be aware that you need to explicitly initialize to manipulate the map.

var m map[string]stringm["a"]="sssss"

The above code will report panic: assignment to entry in nil map that it must be done with the built-in make () function.

m:=make(map[string]string)m["a"]="sssss"

We used a two-dimensional map in my project, and I ran into that problem the first time I wrote it nil map .
The code at the beginning is this:

m:=make(map[string]map[string]string)m["a"]["b"]="ccc"

Later I want to understand if inserting the new element is also a map, you need to make ()!! The revised code is as follows

m:=make(map[string]map[string]int)c:=make(map[string]int)c["b"]=1m["a"]=c

The value of m["a" at this point is another map.

Data access in multi-dimension Map

Map access in one-dimensional situations is simple, and more than two-dimensional situations have to be taken care of. Let's look at an example:

m:=make(map[string]map[string]int)c:=make(map[string]int)c["b"]=1m["a"]=cd:=make(map[string]int)d["c"]=2m["a"]=d

And this time to go to the query m["a"]["b"] will find that the value has not been replaced by m["a"]["c"] .
This is because B and C are both map[string]int types of data, and Golang directly ["a"] replaces the data from B to C, without recursively adding the missing data in the map.
To keep and in M ["a"]["b"] ["a"]["c"] , you need some extra judgment:

if _,exist:=m["a"];exist{    m["a"]["c"]=2}else{    c:=make(map[string]int)    c["c"]=2    m["a"]=c}

In other words, every time you create a one-dimensional map, make () once, or you'll panic. Multi-dimensional map does not add a layer of more make () several times.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.