Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Deleting Keys From Maps | Intro to Structs & Maps
Introduktion till Golang

bookDeleting 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

index.go

copy
12345678910111213
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] }
question mark

Considering the map code below, which of the expressions will execute without compilation errors:

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 7

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you show me a complete example using the delete() function?

What happens if I try to delete multiple keys at once?

Are there any alternatives to the delete() function for removing items from a map?

Awesome!

Completion rate improved to 1.96

bookDeleting Keys From Maps

Svep för att visa menyn

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

index.go

copy
12345678910111213
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] }
question mark

Considering the map code below, which of the expressions will execute without compilation errors:

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 7
some-alt