How to delete or remove element from a Map?
The built-in
delete
function deletes an element from a given map associated with the provided key.
Example
package main
import "fmt"
func main() {
var employee = make(map[string]int)
employee["Mark"] = 10
employee["Sandy"] = 20
employee["Rocky"] = 30
employee["Josef"] = 40
fmt.Println(employee)
delete(employee,"Mark")
fmt.Println(employee)
}
Output
map[Rocky:30 Josef:40 Mark:10 Sandy:20]
map[Josef:40 Sandy:20 Rocky:30]
delete
function used to delete first element from employee
map by passing key Mark
as second argument in delete function.
Most Helpful This Week
How do you write multi-line strings in Go?
Dereferencing a pointer from another package
How to create thumbnail of an image?
How to convert Colorful PNG image to Gray-scale?
How to get current IP form ipify.org ?
How to fix race condition using Atomic Functions in Golang?
Runtime package variables
How to find out element position in slice?
How to check UPPERCASE characters in a string in Golang?
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time