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.8k Golang : How to create new XML file ?
+18.9k Golang : Count number of digits from given integer value
+4.7k Javascript : How to refresh page with JQuery ?
+29.3k Golang : How to convert(cast) string to IP address?
+7.5k Golang : How to join strings?
+2k Golang : Fix go-cron set time not working issue
+7.6k Linux/Unix : fatal: the Postfix mail system is already running
+53.2k Golang : Unmarshal JSON from http response
+13.5k Golang : Rename directory
+6.8k Golang : Check from web if Go application is running or not
+14.1k Golang : How to login and logout with JWT example
+6.9k Golang : Gomobile init produce "iphoneos" cannot be located error