GO语言如何改变地图指针内对象的值
How can I do it?
I have the list of objects, I want list all and change the name of object. I have the list and I'm doing a while end send to another function, there I change the name, but the name doesn't save.
Any idea how can I do it?
https://play.golang.org/p/el3FtwC-3U
And if there is any book that I can read to learn more, please. Thank for helping me =D
我该怎么做? p>
我有对象列表, 我要列出所有对象并更改对象的名称。 我有列表,并且我正在做一会儿发送到另一个函数,在那里我更改了名称,但是该名称没有保存。 p>
任何想法我该怎么做? p>
https://play.golang.org/p/el3FtwC-3U p>
如果有什么书可以阅读以了解更多信息 , 请。 感谢您帮助我= D p> div>
In the range loop:
for _, track := range tracks {
// send track to channel to change the name
Working(&track, &c)
}
the track
variable is actually a copy of the value contained in the map, because the assignement here works on the Track
value type, and in Go, values are copied when assigned.
What you should do instead is use the key of your map and assign the values from within the loop.
for key := range tracks {
t := tracks[key]
// send track to channel to change the name
Working(&t, &c)
tracks[key] = t
}
I didn't found how to get pointer to value in map so I think you have to use map[string]*Track
instead and then work with pointers to Track
structure in your Working
function.
See https://play.golang.org/p/2XJTcKn1md
If you are trying modify tracks in parallel you are may be looking for something like this https://play.golang.org/p/1GhST34wId
Note missing buffer in chanel and go Working
in for range tracks
.