Skip to content

proposal: Go 2: builtin: delete should return the deleted map item #51405

Closed as not planned
@riobard

Description

@riobard

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)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions