-5

I have the following code:

package main

import "fmt"

func main() {
    var m = make(map[string]int)
    m["x-edge-location"] = 10
    m["x-edge-request-id"] = 20
    m["x-edge-response-result-type"] = 30
    m["x-edge-result-type"] = 40

    fmt.Println(m)

    delete(m, "x-edge-result-type")
    delete(m, "x-edge-location")
    fmt.Println(m)
}

is this the only way to remove multiple items from the map?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
khinester
  • 3,398
  • 9
  • 45
  • 88
  • 2
    How else would you like to do it? `delete` only takes one key, so you'll need to call it as many times as you have elements to delete. You can loop over a slice of keys to delete, but that's pretty much the same. – Marc Jul 21 '20 at 09:24
  • 1
    According to the blog there's only one way to delete keys, and that's on a per key basis. https://blog.golang.org/maps https://stackoverflow.com/questions/1736014/delete-mapkey-in-go – timbillstrom Jul 21 '20 at 09:27
  • Yes it's simple if you need to delete multiple item write a variadic function by own and use. – Eklavya Jul 21 '20 at 09:28
  • ok, i see, thank you – khinester Jul 21 '20 at 09:31
  • 1
    Depending on the number of elements you need to delete, it may be easier to create a new array with the elements you want to *keep*. – Rob Jul 21 '20 at 09:39

2 Answers2

2

[I]s this the only way to remove multiple items from the map?

Yes.

Volker
  • 40,468
  • 7
  • 81
  • 87
-2

The only other way to remove multiple items is by iterating through the map. This would remove all items, but you can wrap delete in some if to match your pattern:

package main

import "fmt"

func main() {
    var key string
    var m = make(map[string]int)
    m["x-edge-location"] = 10
    m["x-edge-request-id"] = 20
    m["x-edge-response-result-type"] = 30
    m["x-edge-result-type"] = 40

    fmt.Println(m)

    for key = range m {
        delete(m, key)
    }

    fmt.Println(m)
}
Beerus
  • 34
  • 8
  • That's the same. You're still calling `delete` for each key to be deleted. – Jonathan Hall Jul 21 '20 at 09:53
  • 1. Compared to the OP's example, this is objectively _not_ cleaner. Although for a large list of keys, a loop does make sense. 2. Sure filters can be useful, but the OP didn't ask about that. 3. The OP doesn't want to call `delete` multiple times. In that regard, this absolutely is the same. – Jonathan Hall Jul 21 '20 at 11:28
  • 1. You're arguing that calling 10, 100, 1000 `delete` does not look cleaner than iterating through the loop? 2. Yes, you can use filters and it will work better than calling 10, 100, 1000 times `delete` manually. 3. In my example `delete` is written exactly one time. To be precise, OP asked: " to remove multiple items from the map" - OP did not mention anything about "call `delete` multiple times', thus your comment isn't valid. You're making your story here. Look at the initial question. – Beerus Jul 21 '20 at 12:04
  • 1. No. I'm arguing that 2 `delete`s is cleaner than a loop. 3. How many times you write something doesn't matter to actual complexity. – Jonathan Hall Jul 21 '20 at 12:14
  • Again, the OP question was: "is this the only way to remove multiple items from the map?" Answer with the loop helps to understand that - no, it's not the only way to remove items from the map and yes, in future, when you'll need to remove more than 2 items, e.g 1000, you will use loop, not 1000 lines with `delete` statements. And back again, OP did not say "call `delete` multiple times", you made that up to support your argument. End of discussion. – Beerus Jul 21 '20 at 12:25
  • It is the only way. Doing it multiple times in a loop doesn't change it. – Jonathan Hall Jul 21 '20 at 12:28
  • You're again arguing that writing 1000 `delete` statements manually is the only way, and you should avoid loops. This is incorrect. So no, it's not the only way. Do you want me to send you an example with the code which has 1000 lines of `delete` statements, and other which has loop to iterate through? Which one will be cleaner? You're still up with your idea that OP asked "call `delete` multiple times", which was not the question. So you're made that up and now arguing against it. – Beerus Jul 21 '20 at 12:32
  • "You're again arguing that writing 1000 delete statements manually" I have never made such a claim. – Jonathan Hall Jul 21 '20 at 13:25
  • Sorry, how did you come up with this: "3. The OP doesn't want to call delete multiple times." Can you comment more on how did you make that up from OP question, so I could understand your point and logic. – Beerus Jul 21 '20 at 18:18