Exclusão de Chaves de Mapas
Também é possível remover pares chave-valor de mapas utilizando a função delete().
delete(mapName, keyName)
Alguns pontos importantes sobre a função delete():
- A função
deletenão retorna nenhum valor; - Se a chave
keyNamenão existir, simplesmente nada acontece; - Se o mapa
mapNamenão existir, ocorre um erro durante a compilação.
A seguir, um exemplo de uso da função delete() em um programa:
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] }
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Can you show me a complete example using the delete() function?
What happens if I try to delete a key from a nil map?
Are there any alternatives to the delete() function for removing items from a map?
Awesome!
Completion rate improved to 1.96
Exclusão de Chaves de Mapas
Deslize para mostrar o menu
Também é possível remover pares chave-valor de mapas utilizando a função delete().
delete(mapName, keyName)
Alguns pontos importantes sobre a função delete():
- A função
deletenão retorna nenhum valor; - Se a chave
keyNamenão existir, simplesmente nada acontece; - Se o mapa
mapNamenão existir, ocorre um erro durante a compilação.
A seguir, um exemplo de uso da função delete() em um programa:
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] }
Obrigado pelo seu feedback!