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
+18.9k Golang : Padding data for encryption and un-padding data for decryption
+6k PageSpeed : Clear or flush cache on web server
+16.9k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+28.7k Golang : Detect (OS) Operating System
+7.4k Golang : Rot13 and Rot5 algorithms example
+6.2k Golang : Extract sub-strings
+12.4k Golang : Forwarding a local port to a remote server example
+20.1k Golang : Determine if directory is empty with os.File.Readdir() function
+5.5k PHP : Fix Call to undefined function curl_init() error
+5.6k Unix/Linux/MacOSx : Get local IP address
+6k Golang : How to verify input is rune?
+6k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)