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
+7.8k Yum Error: no such table: packages
+10.9k Golang : Pagination with go-paginator configuration example
+32.7k Golang : convert(cast) bytes to string
+27.8k Golang : Get first few and last few characters from string
+8.1k Golang : How to check if a string with spaces in between is numeric?
+4.5k Python : Delay with time.sleep() function example
+6.3k Golang : Use modern ciphers only in secure connection
+4.7k Python : Print unicode escape characters and string
+4.9k Unix/Linux : How to test user agents blocked successfully ?
+12.3k Golang : reCAPTCHA example
+22.5k Golang : Use regular expression to validate domain name
+14.9k Golang : Delete files by extension