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
+18.4k Golang : Read binary file into memory
+10k Golang : Read file and convert content to string
+13.2k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+14.3k Golang : Get uploaded file name or access uploaded files
+5.2k Golang : Experimental Jawi programming language
+5.3k Golang : PGX CopyFrom to insert rows into Postgres database
+11.2k Golang : Calculate Relative Strength Index(RSI) example
+19.4k Golang : How to count the number of repeated characters in a string?
+22.3k Golang : Convert seconds to minutes and remainder seconds
+19k Golang : Padding data for encryption and un-padding data for decryption
+17.4k Golang : Check if IP address is version 4 or 6