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
+7.1k Golang : Gorrila mux.Vars() function example
+11.4k Golang : Handle API query by curl with Gorilla Queries example
+15.3k Golang : Find location by IP address and display with Google Map
+10k Golang : Get login name from environment and prompt for password
+6.9k Web : How to see your website from different countries?
+11.1k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+9.3k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+8.2k Golang : HttpRouter multiplexer routing example
+9.7k Random number generation with crypto/rand in Go
+4.4k Java : Generate multiplication table example
+5.2k Responsive Google Adsense
+10k Golang : Identifying Golang HTTP client request