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
+6.9k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+4.3k Javascript : Change page title to get viewer attention
+16.1k Golang : Qt image viewer example
+61.4k Golang : Convert HTTP Response body to string
+10.5k Golang : Change date format to yyyy-mm-dd
+6.7k Golang : Reverse a string with unicode
+7.4k Golang : Count leading or ending zeros(any item of interest) example
+3.9k PHP : Extract part of a string starting from the middle
+35.1k Golang : Convert date or time stamp from string to time.Time type
+3.7k Mac OSX : Get disk partitions' size, type and name
+11.6k Golang : Pass database connection to function called from another package and HTTP Handler
+29.7k Golang : Get local IP and MAC address