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
+11.2k Swift : Convert (cast) Float to String
+8.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+7.4k Setting $GOPATH environment variable for Unix/Linux and Windows
+4.8k Golang : Print instead of building pyramids
+5.8k Golang : Create new color from command line parameters
+20.7k Golang : Create and resolve(read) symbolic links
+10.6k Golang : Create Temporary File
+5.1k Swift : Convert string array to array example
+6.7k Golang : Get Alexa ranking data example
+9.7k Golang : Read file and convert content to string
+19.6k Golang : Check if os.Stdin input data is piped or from terminal