Deleting Keys From Maps
We can also remove key-value pairs from maps using the delete()
function.
delete(mapName, keyName)
Here are some points to note regarding the delete()
function:
The
delete
function does not return any value;If the key
keyName
doesn't exist, it simply does nothing;If the map
mapName
doesn't exist, it shows an error during compilation.
Here is an example of the delete()
function being used in a program:
index.go
99
1
2
3
4
5
6
7
8
9
10
11
12
13
package main
import "fmt"
func main() {
var numbers = map[string]int {
"one": 1,
"two": 2,
"three": 3,
}
fmt.Println(numbers) // Output: map[one:1 three:3 two:2]
delete(numbers, "three")
fmt.Println(numbers) // Output: map[one:1 two:2]
}
12345678910111213package main import "fmt" func main() { var numbers = map[string]int { "one": 1, "two": 2, "three": 3, } fmt.Println(numbers) // Output: map[one:1 three:3 two:2] delete(numbers, "three") fmt.Println(numbers) // Output: map[one:1 two:2] }
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 6. Capítulo 7
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo