Skip to content
New issue

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

q014最后一题的解释不通啊 #29

Open
amw863 opened this issue Nov 14, 2020 · 1 comment
Open

q014最后一题的解释不通啊 #29

amw863 opened this issue Nov 14, 2020 · 1 comment

Comments

@amw863
Copy link

amw863 commented Nov 14, 2020

type Student struct {
Age int
}

func main() {
kv := map[string]*Student{"a":{Age:21}}
kv["a"].Age = 22
fmt.Println(kv)
}
这样修改为啥不报错了呢

@hayden-pan
Copy link

是的,原题解释是有问题的,但是在这个项目里的另一个地方有同样的一道题,解释却是对的了。大概意思也是vscode智能提示里的错误,就是map索引读出是不可以获取到地址的。这里第一个回答解释了。这应该是golang故意设计的,因为如果暴露了值的地址,那么用户可以取这个值的地址进行传播,但是当map进行扩容的时候,值的存储地址就会被移动,那么原来的地址会失效。

但是有一点我却不是很理解,同样是在扩容的时候值的地址会变化的slice数据结构,为什么索引取值的时候却可以取地址?而不是和map一样的设计呢?当然如此这样设计,在编写代码的时候就显得很麻烦了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants