How to Delete or Remove keys in a Map in Go

Created
Modified

Using delete Function

Syntax

The syntax of the delete function is shown below.

func delete(m map[Type]Type1, key Type)

delete Usage

The delete built-in function deletes the element with the specified key (m[key]) from the map. If m is nil or there is no such element, delete is a no-op.

See the following example:

package main

import "fmt"

func main() {
  m := map[string]int{
    "a": 1,
    "b": 1,
    "c": 1,
    "d": 1,
  }

  // To delete a map entry
  delete(m, "c")
  fmt.Println(m)
}
map[a:1 b:1 d:1]

Related Tags

#delete# #key# #map#