We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
深度解密 Go 语言之 map
https://qcrao.com/post/dive-into-go-map/
The text was updated successfully, but these errors were encountered:
makemap 和 makeslice 的区别,带来一个不同点:当 map 和 slice 作为函数参数时,在函数参数内部对 map 的操作会影响 map 自身;而对 slice 却不会(之前讲 slice 的文章里有讲过)。
主要原因:一个是指针(*hmap),一个是结构体(slice)。Go 语言中的函数传参都是值传递,在函数内部,参数会被 copy 到本地。*hmap指针 copy 完之后,仍然指向同一个 map,因此函数内部对 map 的操作会影响实参。而 slice 被 copy 后,会成为一个新的 slice,对它进行的操作不会影响到实参。
关于文中这段话的一些补充: 在对新的 slice 操作的时候,如果新的 slice 没有扩容的情况下,复制出来 copy 出来的 slice 结构体的 array 指针值没变,所以操作的时候还是会影响实参,所以会出现不可预知的状况。
Sorry, something went wrong.
No branches or pull requests
深度解密 Go 语言之 map | qcrao 的博客
深度解密 Go 语言之 map
https://qcrao.com/post/dive-into-go-map/
The text was updated successfully, but these errors were encountered: