A
A
Alex Plotnikov2018-07-30 16:16:07
go
Alex Plotnikov, 2018-07-30 16:16:07

How to remove elements from slice []string containing specific text?

Greetings, stuck on a seemingly simple task.
there is a slice []string
for example
[zip_243234, html_2222, zip_123123, zip_6576767, css_2134234]
how to make the slice become
[zip_243234, zip_123123, zip_6576767]
?
Thank you)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2018-07-30
@TrueDevs

Nice set of operations on slices: https://github.com/golang/go/wiki/SliceTricks
This problem needs "Filtering without allocating"

b := a[:0]
for _, x := range a {
  if x != "html_2222" && x != "css_2134234" {
    b = append(b, x)
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question