As a preface I'm coming from python, where I would have a list with my values instead of string slices and do "if x in list". Please tell me if I'm doing this wrong.
I need to take input from the user, if it's contained in my sliced string then break out of my loop and continue, otherwise prompt the user again. Right now I have this
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
foundfdb := false
fdbslices := []string{"f", "d", "b", "files", "directories", "both"}
for {
fmt.Print("Files, Directories, or Both: ")
fdb, _ := reader.ReadString('
')
fdb = strings.ToLower(fdb)
for i := range fdbslices {
if strings.ContainsAny(fdbslices[i], fdb) {
foundfdb = true
}
}
if foundfdb = true {
break
}
}
}
It seems ContainsAny() is giving me some false positives. There's got to be an easier way to do this, or is there?
ContainsAny
allows you to look for a specific character is in a specific string, and I do not think that's what you want. For one of my projects, I implemented a function to look for a string in a slice:
func InArray(a []string, e string) bool {
for _, x := range a {
if x == e {
return true
}
}
return false
}