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
+13.2k Golang : Qt progress dialog example
+9.4k Golang : interface - when and where to use examples
+9k Golang : How to control fmt or log print format?
+10.7k Golang : Simple image viewer with Go-GTK
+6.1k Unix/Linux : Use netstat to find out IP addresses served by your website server
+16.1k Golang :Trim white spaces from a string
+51.9k Golang : How to get struct field and value by name
+7.1k Golang : Scanf function weird error in Windows
+21.9k Golang : Repeat a character by multiple of x factor
+19.3k Golang : Close channel after ticker stopped example
+9.4k Random number generation with crypto/rand in Go
+7.5k Golang : Ways to recover memory during run time.