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
+16.1k Golang : Read large file with bufio.Scanner cause token too long error
+11.9k Golang : Secure file deletion with wipe example
+6.1k Javascript : Get operating system and browser information
+7.9k Golang : Load DSA public key from file example
+9k Golang : GMail API create and send draft with simple upload attachment example
+13.8k Golang : Set image canvas or background to transparent
+5.3k Golang : The Tao of importing package
+12.5k Golang : Encrypt and decrypt data with x509 crypto
+9k Android Studio : Image button and button example
+6.1k Golang : Compound interest over time example
+19k Golang : How to make function callback or pass value from function as parameter?
+11.6k Golang : Change date format to yyyy-mm-dd