Golang : How to delete element(data) from map ?
Problem :
You need to delete some elements from a map.
Such as:
cities := map[string]string{ "city1":"New York", "city2":"Adelaide" };
and remove element "city2" from cities map
Solution :
Use the Golang's builtin function
delete(m, k) // remove element m[k] from map m
For example :
package main
import "fmt"
func main() {
cities := map[string]string{"city1": "New York", "city2": "Adelaide"}
fmt.Println("Before : ", cities) // before delete
delete(cities, "city2")
fmt.Println("After : ", cities) // after delete
}
Output :
Before : map[city1:New York city2:Adelaide]
After : map[city1:New York]
References :
See also : Golang : Check if element exist in map
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+27.1k Golang : dial tcp: too many colons in address
+8.3k Golang : Another camera capture GUI application with GTK and OpenCV
+6.1k Golang : Extract sub-strings
+16.9k Golang : Find file size(disk usage) with filepath.Walk
+10.1k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+8.7k Golang : Build and compile multiple source files
+13.8k Golang : Google Drive API upload and rename example
+6.6k Fix sudo yum hang problem with no output or error messages
+12.9k Golang : How to get a user home directory path?
+6.1k Golang : Test input string for unicode example
+19k Golang : Delete item from slice based on index/key position
+21.6k Golang : Use TLS version 1.2 and enforce server security configuration over client