funccontainsany()在Go中如何工作?



考虑以下代码

func main() {
arg := os.Args
if len(arg[1]) != 1 || len(arg) != 2 {
fmt.Println("Give me a letter.")
return
}
if (strings.IndexAny(arg[1], "yw") == 0) {
fmt.Printf("%q is a semivowel.n", arg[1])
} else if strings.IndexAny(arg[1], "aeiou") == 0 {
fmt.Printf("%q is a vowel.n", arg[1])
} else {
fmt.Printf("%q is a consonant.n", arg[1])
}
}

,更具体地说:

if (strings.IndexAny(arg[1], "yw") == 0) {
fmt.Printf("%q is a semivowel.n", arg[1])
} else if strings.IndexAny(arg[1], "aeiou") == 0 {
fmt.Printf("%q is a vowel.n", arg[1])
} else {
fmt.Printf("%q is a consonant.n", arg[1])
}

我不明白为什么只有当我通知bool等于0时它才起作用但当等于one时不成立. (根据官方文档https://pkg.go.dev/strings#ContainsAny,它不应该等于1吗,就像真的一样?)

根据文档,您使用的函数应该返回一个整数:

package strings // import "strings"
func IndexAny(s, chars string) int
IndexAny returns the index of the first instance of any Unicode code point
from chars in s, or -1 if no Unicode code point from chars is present in s.

strings.ContainsAny返回布尔值

package strings // import "strings"
func ContainsAny(s, chars string) bool
ContainsAny reports whether any Unicode code points in chars are within s.

你可以通过以下命令从终端读取文档:

go doc strings.IndexAny # or any go function or package you want

最新更新