在python中我有这样的代码

search_words_nonTexas = ["tx", "texas", "houston"]
pass = any(word in title for word in search_words_nonTexas)

在Go中,我一直在尝试
firstPass := strings.ContainsAny("title", searchWordsNonTexas)

我收到关于参数不正确的错误(如下所示)。 Go相当于什么?
cannot use searchWordsNonTexas (type [10]string) as type string in
argument to strings.ContainsAny

最佳答案

在python中,我有代码。 Go中的等效功能是什么?


在较低的语言Go中,编写您自己的函数。

例如,

package main

import (
    "fmt"
    "strings"
    "unicode"
)

// Look for list of words in a sentence.
func WordsInSentence(words []string, sentence string) []string {
    var in []string

    dict := make(map[string]string, len(words))
    for _, word := range words {
        dict[strings.ToLower(word)] = word
    }

    f := func(r rune) bool { return !unicode.IsLetter(r) }
    for _, word := range strings.FieldsFunc(sentence, f) {
        if word, ok := dict[strings.ToLower(word)]; ok {
            in = append(in, word)
            delete(dict, word)
        }
    }

    return in
}

func main() {
    words := []string{"tx", "texas", "houston"}
    sentence := "The Texas song Yellow Rose of Texas was sung in Houston, TX."
    in := WordsInSentence(words, sentence)
    fmt.Println(in)
}

游乐场:https://play.golang.org/p/CwSLiDnq928

输出:
[texas houston tx]

10-04 22:34