Delete key in map

DictionaryGo

Dictionary Problem Overview


I have a map:

var sessions =  map[string] chan int{}

How do I delete sessions[key]? I tried:

sessions[key] = nil,false;

That didn't work.

Update (November 2011):

The special syntax for deleting map entries is removed in Go version 1:

> Go 1 will remove the special map assignment and introduce a new built-in function, delete: delete(m, x) will delete the map entry retrieved by the expression m[x]. ...

Dictionary Solutions


Solution 1 - Dictionary

Go introduced a delete(map, key) function:

package main

func main () {
    var sessions = map[string] chan int{};
    delete(sessions, "moo");
}

Solution 2 - Dictionary

Copied from Go 1 release notes

In the old language, to delete the entry with key k from the map represented by m, one wrote the statement,

m[k] = value, false

This syntax was a peculiar special case, the only two-to-one assignment. It required passing a value (usually ignored) that is evaluated but discarded, plus a boolean that was nearly always the constant false. It did the job but was odd and a point of contention.

In Go 1, that syntax has gone; instead there is a new built-in function, delete. The call

delete(m, k)

will delete the map entry retrieved by the expression m[k]. There is no return value. Deleting a non-existent entry is a no-op.

Updating: Running go fix will convert expressions of the form m[k] = value, false into delete(m, k) when it is clear that the ignored value can be safely discarded from the program and false refers to the predefined boolean constant. The fix tool will flag other uses of the syntax for inspection by the programmer.

Solution 3 - Dictionary

From Effective Go:

> To delete a map entry, use the delete built-in function, whose arguments are the map and the key to be deleted. It's safe to do this even if the key is already absent from the map. > > delete(timeZone, "PDT") // Now on Standard Time

Solution 4 - Dictionary

delete(sessions, "anykey")

These days, nothing will crash.

Solution 5 - Dictionary

Use make (chan int) instead of nil. The first value has to be the same type that your map holds.

package main

import "fmt"

func main() {

    var sessions = map[string] chan int{}
    sessions["somekey"] = make(chan int)

    fmt.Printf ("%d\n", len(sessions)) // 1

    // Remove somekey's value from sessions
    delete(sessions, "somekey")

    fmt.Printf ("%d\n", len(sessions)) // 0
}

UPDATE: Corrected my answer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjonazView Question on Stackoverflow
Solution 1 - Dictionaryuser181548View Answer on Stackoverflow
Solution 2 - DictionaryhannsonView Answer on Stackoverflow
Solution 3 - DictionaryGyörgy AndrasekView Answer on Stackoverflow
Solution 4 - DictionaryZippoView Answer on Stackoverflow
Solution 5 - DictionaryIsaiahView Answer on Stackoverflow