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
Example to use Weekday and YearDay function
Golang HTTP GET request with parameters
How to fix race condition using Atomic Functions in Golang?
Strip all white spaces, tabs, newlines from a string
Different ways to convert Byte Array into String
How to blur an image in Golang?
How to use wildcard or a variable in our URL for complex routing?
How to convert Go struct to JSON?
How to use Ellipsis (...) in Golang?
How to use a mutex to define critical sections of code and fix race conditions?