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
+30.7k error: trying to remove "yum", which is protected
+5.2k Golang : Generate Interleaved 2 inch by 5 inch barcode
+8.7k Golang : Random integer with rand.Seed() within a given range
+14.1k Golang : Chunk split or divide a string into smaller chunk example
+25.1k Golang : Convert uint value to string type
+6.6k Golang : Derive cryptographic key from passwords with Argon2
+5.9k Golang : Compound interest over time example
+12.5k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+14.1k Golang : syscall.Socket example
+25.9k Mac/Linux and Golang : Fix bind: address already in use error
+29.7k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+9.8k Golang : Translate language with language package example