How to delete an element from a map in Go? (delete key in map)
Hello, colleagues! I'm a beginner in Go and encountered a problem while working with maps. I need to delete an element by key, but I can't find a suitable method. In Python, I'm used to writing `del dict[key]` or `dict.pop(key)`. In Go, as far as I understand, the `delete()` function is used, but it doesn't behave as I expect.
Here's an example: I have a map with usernames and their ages. I want to delete the entry for the user "Alice". I write:
```go
package main
import "fmt"
func main() {
ages := map[string]int{
"Alice": 30,
"Bob": 25,
"Eve": 35,
}
delete(ages, "Alice")
fmt.Println(ages) // Output: map[Bob:25 Eve:35]
}
```
It seems to work, but I have a few questions:
1. The `delete()` function doesn't return any value. How do I know if the element was actually deleted?
No answers yet.
Log in to leave an answer
Blogs