将嵌套循环内生成的数据存储在地图或切片中

I am coming from PHP background and trying to build small tool that would allow me to filter the list of keywords against the black list both supplied via CSV file. I managed to do that in PHP but it has some speed limitation. So I decided to try my powers with Golang. I have succeeded to open CSV file fmt.Println the contents of both keywords and blacklist, and managed to compare each of the keywords to each of the blacklists keyword. But the problem I face is that I can figure out how to build dynamic associative array something like that.

   function compareKeywords($keyword, $filters) {
        $matches = [];    
        foreach($filters as $filter) {
            $matches[] = strpos($keyword, $filter);
        }    
        $matches = array_filter($matches);    
        if(empty($matches)) {
            return $keyword
        }
    }

This function simply checks keyword against fully or partially matching the blacklisted keywords if it does not match the blacklisted keywords it returns this keyword and then adds it to CSV file.

I spent decent amount of time trying to create similar function in Golang but I struggle to mimic the associative array from PHP.

I came up with slightly different function which accepts keywords rather than single keyword as well as filters and uses nested loop.

func compare(keywords [][]string, filterKeywords [][]string) (keywordMap //no sure what type it should be) {
    matchFilters := make(map[string]string)
    keywordMap :=  make(map[string]string)

    for _, keyword := range keywords {
        for _, filter := range filterKeywords {
            if convStr(keyword) == convStr(filter) {
                // here i want to store matches filters 
            }
        }
    }

    //Check if matchFilters empty and return filtered keywordMap

    return

}

I know my code may look verbose and look a bit lame. But any advice into direction I should move would be very appreciated.

I do not know PHP, but as far as I understand from the PHP code the Go equivalent should be:

func compare(keyword string, filters map[string]struct{}) (string, error) {
    // value, ok := myMap[key]
    // this is Go syntax for checking the existance of a key in a map - check the ok variable.
    _, filterMatched := filters[keyword]
    if filterMatched {
        return ``, fmt.Errorf("keyword %s got filtered", keyword)
    }

    return keyword, nil
}

In other words you could just use the map type, but having a helper function that clears our intention is good.

Here is the working code with some optimizations, try it on the The Go Playground:

package main

import (
    "fmt"
    "strings"
)

func main() {
    filters := []string{"aaa", "bbb", "ccc"}
    fmt.Println(compareKeywords("a", filters)) // ""
    fmt.Println(compareKeywords("d", filters)) // "d"
}
func compareKeywords(keyword string, filters []string) string {
    for _, filter := range filters {
        if strings.Index(filter, keyword) >= 0 {
            return ""
        }
    }
    return keyword
}

output:

d

And see strings.Index Docs:

func Index(s, sep string) int

Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.