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
+10.1k Golang : Edge detection with Sobel method
+51.4k Golang : Check if item is in slice/array
+19.6k Golang : Get current URL example
+22k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+21.7k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+29.9k Golang : Get and Set User-Agent examples
+25.4k Golang : Convert long hexadecimal with strconv.ParseUint example
+12.2k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+5.4k How to check with curl if my website or the asset is gzipped ?
+13.7k Golang : Activate web camera and broadcast out base64 encoded images
+9.9k Golang : Translate language with language package example
+8.8k Golang : On lambda, anonymous, inline functions and function literals