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
+8.9k Android Studio : Simple input textbox and intercept key example
+6.4k Golang : Get YouTube playlist
+4.3k Golang : Return multiple values from function
+13.5k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+20.1k SSL : How to check if current certificate is sha1 or sha2
+10.3k Golang : Generate DSA private, public key and PEM files example
+5.7k Fix sudo yum hang problem with no output or error messages
+15.3k Golang : read gzipped http response
+4.1k Unix/Linux : secure copying between servers with SCP command examples
+5.9k Golang : Mapping Iban to Dunging alphabets
+7.6k Golang : Accept any number of function arguments with three dots(...)
+17.4k Unmarshal/Load CSV record into struct in Go