Friday, 3 May 2019

Go language: Delete item from the map


‘delete’ method is used to delete an item in the map.

Syntax
delete(mapName, key)

App.go
package main

import "fmt"

func main() {

 employees := map[int]string{
  1: "Krishna",
  2: "Ram",
  3: "Chamu", //Must have trailing comma
 }

 fmt.Println("employees : ", employees)

 delete(employees, 2)

 fmt.Println("\nDeleting the item with key 2")

 fmt.Println("\nemployees : ", employees)
}

Output
employees :  map[1:Krishna 2:Ram 3:Chamu]

Deleting the item with key 2

employees :  map[3:Chamu 1:Krishna]




Previous                                                 Next                                                 Home

No comments:

Post a Comment