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
deletefunction does not return any value; - If the key
keyNamedoesn't exist, it simply does nothing; - If the map
mapNamedoesn't exist, it shows an error during compilation.
Here is an example of the delete() function being used in a program:
index.go
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] }
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 6. Hoofdstuk 7
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Suggested prompts:
Stel mij vragen over dit onderwerp
Vat dit hoofdstuk samen
Toon voorbeelden uit de praktijk
Awesome!
Completion rate improved to 1.96
Deleting Keys From Maps
Veeg om het menu te tonen
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
deletefunction does not return any value; - If the key
keyNamedoesn't exist, it simply does nothing; - If the map
mapNamedoesn't exist, it shows an error during compilation.
Here is an example of the delete() function being used in a program:
index.go
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] }
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 6. Hoofdstuk 7