Golang Map addressability

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

http://wangzhezhe.github.io/blog/2016/01/22/golangmapaddressability-dot-md/

Questions about Map Accessibility in Golang (addresable?)

There are examples of this in Go playground:

123456789   
package mainimport (  "fmt")type Type struct { A string}func main() { items := make(map[string]Type) items["q"] = Type{} items["q"].A = "abc" fmt.Println(items)}

This will report a common error when executing:cannot assign to items["q"].A

Change the way value is declared, and then perform a similar operation:

12345678910111213141516          
package mainimport (  "fmt")type Type struct { A string}func main() { items := make(map[string]*Type) items["q"] = &Type{} items["q"].A = "abc" fmt.Printf("%+v", items)}

In the Golang design, the value of the map should be an unreachable address, that is, directly take the address of the element in the map will be error, such as the above example of the main function is changed to the following:

1234  
itemsb := make(map[string]Type)itemsb["p"] = Type{3}pointer := &itemsb["p"]fmt.Println(pointer)

will report cannot take the address of itemsb["p"] the error. This is largely because, in Golang, a map with a growing capacity could cause some elements in the original map to rehashing, allowing them to be reassigned to the new storage location, which could cause the previously obtained address to become unusable. is the so-called map member not addresable.

As the answer says, the map's indexing operation is an unreachable address, which is related to the specific implementation mechanism of map in Golang, and the map in Golang does not guarantee that the address of their value is immutable. Because the address of value values is likely to be reassigned, as previously stated. One way to modify this is to set the value to the form of a pointer, which is equivalent to adding an extra entry, even if the position of the value you really need is changed, and you can redirection the past. This is especially important when you later use map to declare the value of a struct body.

For the Slice index operation is the address can be reached, for the map is unreachable, always use the map when you have to pay special attention.

?? Map is a reference to the map contents, but it does isn't hold references (unless it explicitly stores pointers).

Related references:

Https://golang.org/ref/spec#Address_operators

https://code.google.com/p/go/issues/detail?id=3117

Https://groups.google.com/forum/#!topic/golang-nuts/4_pabWnsMp0

Https://golang.org/ref/spec

Http://stackoverflow.com/questions/13101298/calling-a-pointer-method-on-a-struct-in-a-map https://golang.org/ Ref/spec#address_operators

Http://stackoverflow.com/questions/16059038/can-not-assign-to-a-struct-member-from-map

The first thing to be clear is, at the time of declaration &type{} and type{} The difference between

Currently map member is not addressable.

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.