In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you how to understand the traversal and ranking of dictionary types in the foundation of Go language. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
What is go?
Golang is a compiled language that compiles code into machine code, and the compiled binaries can be deployed directly to the target machine without additional dependencies, so golang outperforms other interpretative languages and can use goroutine to achieve concurrency in golang. It provides a very elegant goroutine scheduler system that can easily generate millions of goroutine.
Ergodic dictionary
We can traverse the field type data as we traverse the data:
TestMap: = map [string] int {"one": 1, "two": 2, "three": 3,}
For key, value: = range testMap {fmt.Println (key, value)}
This traversal mode is very similar to how we traverse the associative array through foreach in PHP. The output of the above code is:
Three 3one 1two 2
Of course, we can also use anonymous variables to get only the value of the dictionary:
For _, value: = range testMap {fmt.Println (value)}
Or just get the key name of the dictionary like this:
For key: = range testMap {fmt.Println (key)} key value exchange
The so-called key-value swap refers to exchanging the keys and values of the dictionary. In the PHP associative array, there is a built-in array function array_flip to achieve similar functions. In the Go language, we need to write code manually to achieve this. For example, if we want to exchange the keys of the testMap dictionary, we can do this:
InvMap: = make (map [int] string, 3)
For k, v: = range testMap {invMap [v] = k}
For k, v: = range invMap {fmt.Println (k, v)}
The print result of the above code is:
3 three1 one2 two dictionary sort
In the previous tutorial, we mentioned that the dictionary of the Go language is an unordered collection, which is different from the associative array of PHP. If you want to sort the dictionary, you can do this by creating slices for the keys and values of the dictionary, and then sorting the slices. In other words, if you want to sort dictionaries by key, you can do this:
Keys: = make ([] string, 0) for k, _: = range testMap {keys = append (keys, k)}
Sort.Strings (keys) / / sort by a pair of keys
Fmt.Println ("Sorted map by key:") for _, k: = range keys {fmt.Println (k, testMap [k])}
The printed result of the above code is:
Sorted map by key:one 1three 3two 2
This result is the result of sorting in ascending order according to the sort of key name in the alphabet.
If you want to sort dictionaries by value, do this:
Values: = make ([] int, 0) for _, v: = range testMap {values = append (values, v)}
Sort.Ints (values) / / sort values
Fmt.Println ("Sorted map by value:") for _, v: = range values {fmt.Println (invMap [v], v)}
Here, with the help of the invMap created earlier, the corresponding keys are checked back through the values of the dictionary. The above code prints as follows:
Sorted map by value:one 1two 2three 3
The result is the result of sorting in ascending order according to the number size corresponding to the key value.
In addition, you may have noticed that when sorting slices, we used the sort package built into the Go language, which provides a series of functions to sort slices and user-defined collections.
This is how to understand the traversal and ordering of dictionary types in the basis of the Go language shared by the editor. If you happen to have similar doubts, please refer to the above analysis. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 252
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.