This is a creation in Article, where the information may have evolved or changed.
The data structure of map in the go language, the following only a few key attributes are extracted:
struct Bucket{ uint8 tophash[BUCKETSIZE]; // top 8 bits of hash of each entry (0 = empty) Bucket *overflow; // overflow bucket, if any byte data[1]; // BUCKETSIZE keys followed by BUCKETSIZE values}; struct Hmap{uint8 B; // log_2 of # of buckets (can hold up to LOAD * 2^B items) byte *buckets; // array of 2^B Buckets. may be nil if count==0. byte *oldbuckets; // previous bucket array of half the size, non-nil only when growing};
The low part of the hash value of key determines the index position of key in buckets.
When the corresponding bucket overflows, the new item is inserted through the overflow in the bucket.
Grow Bucket
When the number of elements in the entire hmap exceeds 65% of the bucket number, the buckets are allocated twice times, and then the elements in each bucket are split into new buckets.
Moving the elements in the old bucket into the new bucket is a gradual process that preserves the old buckets, which brings the process of traversing the map, as well as inserting and deleting, a feature that many other languages do not have.
Shrink Bucket
Hmap also does not support bucket shrink, which causes some programs to use a very large number of maps in a short period of time, after which memory cannot be effectively released.