To remove a specific key-value pair from a map in Golang, we need to use the built-in delete
function. The delete
function takes two arguments: the map and the key. Here's an example:
main.go409 chars22 lines
Output:
main.go68 chars3 lines
In this example, we define a map named myMap
with three key-value pairs. We then print the map before deleting a key-value pair, delete the "banana" key-value pair using the delete
function, and then print the map again to confirm that the "banana" key-value pair has been removed.
gistlibby LogSnag