In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use Go Map correctly". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Go Map correctly.
Preface
Examples are as follows:
Func main () {m: = make (map [int32] string) m [0] = "EDDYCJY1" m [1] = "EDDYCJY2" m [2] = "EDDYCJY3" m [3] = "EDDYCJY4" m [4] = "EDDYCJY5" for k, v: = range m {log.Printf ("k:% v, v:% v", k, v)}}
Suppose you run this code, what is the output? Is it ordered or disordered output?
K: 3, v: EDDYCJY4 k: 4, v: EDDYCJY5 k: 0, v: EDDYCJY1 k: 1, v: EDDYCJY2 k: 2, v: EDDYCJY3
In terms of output, it is output in a non-fixed order, that is, it is different each time. But why?
First of all, I suggest you think about why. Secondly, I heard some comments during the interview. Some people say that because it is a hash, it is out of order and so on. I was a little bit at that time.
This is also the reason for the emergence of this article. I hope you can discuss it together and sort out this problem:)
Take a look at the compilation... 0x009b 00155 (main.go:11) LEAQ type. Map [int 32] string (SB), AX 0x00a2 00162 (main.go:11) PCDATA $2, $0 0x00a2 00162 (main.go:11) MOVQ AX, (SP) 0x00a6 00166 (main.go:11) PCDATA $2, $2 0x00a6 00166 (main.go:11) LEAQ ".. autotmp _ 3 cards 24 (SP), AX 0x00ab 00171 (main.go:11) PCDATA $2, $0 0x00ab 00171 (main.go:11) MOVQ AX. 8 (SP) 0x00b0 00176 (main.go:11) PCDATA $2, $2 0x00b0 00176 (main.go:11) LEAQ ".. autotmp _ 23072 (SP), AX 0x00b5 00181 (main.go:11) PCDATA $2, $0 0x00b5 00181 (main.go:11) MOVQ AX, 16 (SP) 0x00ba 00186 (main.go:11) CALL runtime.mapiterinit (SB) 0x00bf 00191 (main.go:11) JMP 2070x00c1 00193 (main.go:11) PCDATA $2, $2 0x00c1 00193 (main.go:11) LEAQ".. autotmp _ 2percent 72 (SP) AX 0x00c6 00198 (main.go:11) PCDATA $2, $0 0x00c6 00198 (main.go:11) MOVQ AX, (SP) 0x00ca 00202 (main.go:11) CALL runtime.mapiternext (SB) 0x00cf 00207 (main.go:11) CMPQ ".autotmp _ 2y72 (SP), $0 0x00d5 00213 (main.go:11) JNE 193.
Let's take a look at the overall process and focus on the iterations of the Go map loop with two runtime methods, as follows:
Runtime.mapiterinit
Runtime.mapiternext
But you may think, obviously using for range for loop iteration, how did these two functions come into being, what's going on?
Take a look at the converted var hiter map_iteration_struct for mapiterinit (type, range, & hiter); hiter.key! = nil; mapiternext (& hiter) {index_temp = * hiter.key value_temp = * hiter.val index = index_temp value = value_temp original body}
In fact, the compiler has different ways to implement the loop iteration of slice and map, not just throwing for away, but also doing some additional actions to deal with it. The above code is the pseudo implementation of for range map after the compiler is expanded.
Look at the source code.
Runtime.mapiterinit
Func mapiterinit (t * maptype, h * hmap, it * hiter) {. It.t = t it.h = h it.B = h.B it.buckets = h.buckets if t.bucket.kind&kindNoPointers! = 0 {h.createOverflow () it.overflow = h.extra.overflow it.oldoverflow = h.extra.oldoverflow} r: = uintptr (fastrand ()) if h.B > 31-bucketCntBits {r + = uintptr (fastrand ()) > h.B & (bucketCnt-1) it.bucket = it.startBucket. Mapiternext (it)}
By reading the mapiterinit method, we can see that its main purpose is to initialize the action during the traversal iteration of map. There are three formal parameters for reading the type information of the current hash table, the storage information of the current hash table, and the data of the current traversal iteration.
Why
We pay attention to the part of fastrand in the source code, this method name, whether it looks familiar or not. Yes, it's a way to generate random numbers. Look at the context again:
/ / decide where to start r: = uintptr (fastrand ()) if h.B > 31-bucketCntBits {r + = uintptr (fastrand ()) > h.B & (bucketCnt-1)) / / iterator state it.bucket = it.startBucket
In this code, it generates a random number. Used to decide where to start a loop iteration. More specifically, according to the random number, select a bucket position as the starting point for traversal iteration.
So every time you re-for range map, the results you see are different. That's because its starting position is not fixed at all!
Runtime.mapiternext
Func mapiternext (it * hiter) {... For; I < bucketCnt; iTunes + {... K: = add (unsafe.Pointer (b), dataOffset+uintptr (offi) * uintptr (t.keysize)) v: = add (unsafe.Pointer (b), dataOffset+bucketCnt*uintptr (t.keysize) + uintptr (offi) * uintptr (t.valuesize)). If (b.tophash [offi]! = evacuatedX & & b.tophash [offi]! = evacuatedY) | |! (t.reflexivekey | | alg.equal (k, k)) {. It.key = k it.value = v} else {rk, rv: = mapaccessK (t, h, k) if rk = = nil {continue / / key has been deleted} it.key = rk it.value = rv} it.bucket = bucket if it.bptr! = b {it.bptr = b} it.i = I + 1 it.checkBucket = checkBucket return} b = b.overflow (t) I = 0 goto next} I believe you have a deeper understanding of "how to use Go Map correctly". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 0
*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.