Can you delete keys from a map in Golang using a variable that contains the name of the key instead of the key in quotations?
Solution 1:
You named your function delete
, so inside it you shadow the builtin delete()
function, and using it will refer to your delete()
function. Rename it.
Also note that to achieve what you want, you don't need a loop, just use delete(map1, key2)
:
func remove() {
fmt.Println("Enter key to be deleted: ")
var key2 string
fmt.Scanln(&key2)
fmt.Println(map1)
delete(map1, key2)
fmt.Println(map1)
}