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
+9.5k Swift : Convert (cast) String to Integer
+45.4k Golang : Convert int to byte array([]byte)
+29.2k Golang : How to redirect to new page with net/http?
+6.6k Golang : Generate human readable password
+40.8k Golang : Get hardware information such as disk, memory and CPU usage
+12.5k Golang : How to check if a file is hidden?
+29.3k Golang : Download file example
+2.8k Java : Random alphabets, alpha-numeric or numbers only string generator
+13.1k Golang : GUI with Qt and OpenCV to capture image from camera
+28.9k Golang : Calculate percentage change of two values
+4.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+12.7k Golang : How to convert a number to words