How func ContainsAny() works in Go?

huangapple go评论70阅读模式
英文:

How func ContainsAny() works in 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])
}

我无法理解为什么只有当我将布尔值设置为时才能正常工作,而不是设置为。(根据官方文档https://pkg.go.dev/strings#ContainsAny,它不应该等于1,就像true一样吗?)

根据您提供的代码,strings.IndexAny函数返回的是匹配字符的索引位置,如果没有匹配到,则返回-1。在这种情况下,如果匹配到的索引位置为0,表示第一个字符匹配成功。

因此,当您将布尔值设置为0时,它会执行与匹配到第一个字符的情况相对应的代码块。而当您将布尔值设置为1时,它不会执行与匹配到第一个字符的情况相对应的代码块,因为索引位置不是1。

所以,根据您的代码逻辑,将布尔值设置为0才能使代码正常工作。

英文:

Considering the following code

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])
    	}
}

and more specifically this section:

    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])
    }

I could not understand why it only worked when I informed the bool equal to zero but not when equal to one. (According to the official documentation https://pkg.go.dev/strings#ContainsAny shouldn't it be equal to 1, as in true?)

答案1

得分: 1

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

package strings // 导入 "strings" 包

func IndexAny(s, chars string) int
    IndexAny 函数返回 chars 中任意一个 Unicode 代码点在 s 中第一次出现的索引如果 s 中没有 chars 中的任何 Unicode 代码点则返回 -1

strings.ContainsAny 返回一个布尔值:

package strings // 导入 "strings" 包

func ContainsAny(s, chars string) bool
    ContainsAny 函数报告 chars 中的任何 Unicode 代码点是否在 s 

你可以在终端上运行以下命令来阅读文档:

go doc strings.IndexAny # 或者任何你想要的 go 函数或包
英文:

According to the documentation, the function you used should return an integer:

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.

and strings.ContainsAny returns a boolean

package strings // import "strings"

func ContainsAny(s, chars string) bool
    ContainsAny reports whether any Unicode code points in chars are within s.

You can read the documentation from a terminal running the command:

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

huangapple
  • 本文由 发表于 2021年8月22日 05:14:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/68876801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定