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
+10.2k Golang : Post data with url.Values{}
+9k Golang : Use regular expression to get all upper case or lower case characters example
+9.9k Golang : Intercept and process UNIX signals example
+5k Facebook : How to force facebook to scrape latest URL link data?
+7.3k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+8.4k Golang : Qt get screen resolution and display on center example
+9.2k Golang : cannot assign type int to value (type uint8) in range error
+8.3k Golang : How to get ECDSA curve and parameters data?
+5.7k PHP : Shuffle to display different content or advertisement
+11.6k Golang : Transform comma separated string to slice example
+29.3k Golang : How to convert(cast) string to IP address?
+10.4k Golang : Secure file deletion with wipe example