Complex usage of Go language map

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. For Jane's map such as map[string] string is still very well mastered.
The following procedure demonstrates a complex map

Package Main

Import "FMT"

Type PersonInfo struct {
ID string
Name string
Address string
}

Func Main () {

PERSONDB: = Make (Map[string][2]personinfo)

Initialize, note the initialization of the array
persondb["test1"] = [2]personinfo{{"12345", "Tom", "AAA"}, {"12346", "Xym", "BBB"}}

Element lookup, which is a common use method
V, OK: = persondb["Test1"]
If!ok {
Fmt. PRINTLN ("No Information Found")
Return
}

Print out all values and each score
Fmt. Printf ("V=%v v[0]=%v v[1]=%v \ n", V, V[0], v[1])

Use range to get the value easily
For I, V: = Range v {

Fmt. Println (i, V, v.id, v.address, V.name)

can do other processing.
}
}


Output Result:

v=[{12345 Tom AAA} {12346 Xym bbb}] v[0]={12345 Tom AAA} v[1]={12346 Xym BBB}
0 {12345 Tom AAA} 12345 AAA Tom
1 {12346 Xym bbb} 12346 BBB Xym


Related Article

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.