Task: Provide an array of string exceptions, an array of string extensions as an argument to the if statement.

Let's say i have

fileExt := []string{".exe", ".bin"} fileExcept := []string{"waste.exe", "unusual.exe"} 

I cannot do an iteration of the form for _, fext := range fileExt {} since under this iteration there will be several more iterations, and instead of one or two iterations passed, there will be over a hundred, depending on how many arguments are specified in [] string arrays. I want to do something like lookup for values ​​in an array, that is, if file.Ext == searchExts([]string{".exe", ".bin"}) -> if the found files have the extension .exe OR .bin , no matter in what sequence, will return the result true. I have a searchString function, but it can only search for 1 file extension, not a list of extensions, here’s the code:

 func searchString(files []string, target string) string { var cFiles string sort.Strings(files) i := sort.SearchStrings(files, target) if i < len(files) && files[i] == target { cFiles = files[i] } return cFiles } 

    1 answer 1

    If you need a constant 1 search by file extension, then do so:

     exts := map[string]bool{ ".exe": true, ".txt": true, } files := []string{ "foo.txt", "bar.exe", "baz.xml", } for _, f := range files { ext := filepath.Ext(f) if exts[ext] { fmt.Printf("%s matches\n", f) continue } fmt.Printf("%s doesn't match\n", f) } 

    Playground: https://play.golang.org/p/2_SG1E6X63X .

    Of the other options, except regular expressions.

    1. Amortized.