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.4k Golang : Command line ticker to show work in progress
+12k Golang : Simple client-server HMAC authentication without SSL example
+17.4k How to enable MariaDB/MySQL logs ?
+9.5k Golang : Populate slice with sequential integers example
+21.9k Golang : Repeat a character by multiple of x factor
+8.6k Android Studio : Image button and button example
+15.1k Golang : Delete certain files in a directory
+8.2k Golang : Generate Datamatrix barcode
+37.7k Golang : Read a text file and replace certain words
+17.7k Golang : How to log each HTTP request to your web server?
+6.9k Golang : Get Alexa ranking data example
+10.8k Golang : Generate random elements without repetition or duplicate