通过值或引用返回的golang地图值? [重复]

This question already has an answer here:

let's I have a map with key of string and value of pointer to a struct

type Entity struct{}
entity := &Entity{}
m := map[string]*Entity{"foo":entity}

I get the value and delete the entry later

en := m["foo"]
delete(m, "foo")

my question is that will the memory be garbage collected since the pointer to it is removed from the map? On the other note, I have the new pointer en before I delete the entry, it's a copy of the pointer also points to the same memory location, so the memory will not be garbage collected even the map entry is deleted? I am confused, help appreciated.

</div>

Go's current implementation is a parallel mark-and-sweep garbage collector.

To be precise on the scope where entity is declared, this object won't be garbaged collected, by the same reason you mentioned - after the deletion from the map there still is a reference to the object.

The state of the map is internal, and it will only be removed from there after the deletion operation.