Closed as not planned
Description
Currently the builtin delete
function has the signature
func delete(m map[Type]Type1, key Type)
A common usage pattern is to get an item from a map and then remove it after processing:
doSomethingWith(m[k])
delete(m, k)
which involves hashing k
and locating it in m
twice.
If delete
is changed to
func delete(m map[Type]Type1, key Type) Type1
then getting an item and removing it from a map can be done in one step:
doSomethingWith(delete(m, k))
This is similar to Python's dict.pop(key)
method. The proposal changes the builtin function's API but I don't think it would break any existing code, so Go1 compatibility should not be an obstacle.
Additionally, as suggested below by @ydnar, delete
could be further enhanced with comma-ok pattern:
v, ok := delete(m, k)
if ok {
doSomethingWith(v)
}