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
+23.4k Golang : Fix type interface{} has no field or no methods and type assertions example
+22.7k Golang : Randomly pick an item from a slice/array example
+22.3k Generate checksum for a file in Go
+21.8k Golang : How to run Golang application such as web server in the background or as daemon?
+15.4k Golang : How to login and logout with JWT example
+6.5k Golang : Reverse by word
+12.8k Swift : Convert (cast) Int to String ?
+6.9k Golang : Use modern ciphers only in secure connection
+9.9k Golang : Compare files modify date example
+7.7k Golang : Check from web if Go application is running or not
+5.3k PHP : Fix Call to undefined function curl_init() error
+27.1k PHP : Convert(cast) string to bigInt