If a map isn ' t a reference variable, what's it?

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

In my previous post I showed that Go maps is not reference variables, and is not passed by reference. This leaves the question, if maps is not references variables, what is they?

For the impatient, the answer is:

A map value is a pointer to a runtime.hmap structure.

If you ' re not satisfied with this explanation, read on.

What is the type of a map value?

When you write the statement

M: = Make (Map[int]int)

The compiler replaces it with a call runtime.makemap to, which have the the signature

Makemap implements a Go map creation make (MAP[K]V, hint)/If The compiler have determined that the map or the first Buc ket//can created on the stack, h and/or buckets may be non-nil.//If h = nil, the map can be created directly in h.// If Bucket! = nil, bucket can be used as the first bucket.func makemap (t *maptype, hint Int64, h *hmap, bucket unsafe. Pointer) *hmap

As you see, the type of the value returned from was runtime.makemap a pointer to a runtime.hmap structure. We cannot see this from normal Go code, but we can confirm that a map value are the same size as a uintptr –one machine word.

Package Mainimport ("FMT" "unsafe") Func main () {var m map[int]intvar p Uintptrfmt. Println (unsafe. Sizeof (m), unsafe. Sizeof (P))//8 8 (LINUX/AMD64)}

If maps is pointers, shouldn ' t they be *map[key]value?

It's a good question that if maps is pointer values, why does the expression make(map[int]int) return a value with the type map[int]int . Shouldn ' t it return a *map[int]int ? Ian Taylor answered this recently in a golang-nuts thread1.

In the very early days, "We call" Maps now were written as pointers, so you wrote *map[int]int. We moved away from if we realized that no one ever wrote ' map ' without writing ' *map '.

Arguably renaming the type from " map[int]int map[int]int to" while confusing because the type does not "look like a pointer, W As less confusing than a pointer shaped value which cannot is dereferenced.

Conclusion

Maps, like channels, but unlike slices, is just pointers to runtime types. As you saw above, a map is just a pointer to a runtime.hmap structure.

Maps has the same pointer semantics as any and pointer value in a GO program. There is no magic save the rewriting of maps syntax by the compiler into calls to functions in runtime/hmap.go .

Notes

    1. If you look over enough back in the history of Go repository, you can find examples for maps created with the new operator.

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.